From 66e759ac5698b746967625dba30dc3f3f5ad5151 Mon Sep 17 00:00:00 2001 From: Samuel Plumppu Date: Sat, 18 Feb 2017 18:18:23 +0100 Subject: [PATCH] fix(challenge): Improve tests for Circular Queue challenge. (#13308) * Fix some typos that prevented valid solutions from passing. * Add new test cases to better test for what the instructions ask for. * Improve test-messages. * Add passing solution, to increase coverage of `npm test`. --- .../coding-interview-data-structure-questions.json | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json index 43520b8492..625a18ba3a 100644 --- a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json +++ b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json @@ -214,11 +214,18 @@ " }" ], "tests": [ - "assert((function(){var test = new CircularQueuee(); test.enqueue('a'); test.enqueue('b'); test.enqueue('c'); test.enqueue('d'); test.enqueue('e'); test.enqueue('f'); return (test.printCollection === ['f', 'b', 'c', 'd', 'e'])}()), 'message: Your Queue class should have a enqueue method.');", - "assert((function(){var test = new CircularQueuee(); test.enqueue('a'); test.enqueue('b'); test.enqueue('c'); test.enqueue('d'); test.enqueue('e'); test.enqueue('f'); test.dequeue; return (test.printCollection === ['', 'b', 'c', 'd', 'e'])}()), 'message: Your dequeue method did not remove the first item.');" + "assert(typeof new CircularQueue().enqueue === 'function', 'message: Your CircularQueue class should have a enqueue method.');", + "assert.deepEqual((function(){var test = new CircularQueue(); test.enqueue('a'); test.enqueue('b'); test.enqueue('c'); test.enqueue('d'); test.enqueue('e'); test.enqueue('f'); return test.printCollection(); })(), ['f', 'b', 'c', 'd', 'e'], 'message: Your enqueue method should add items to the position of the write pointer, looping back to the beginning once the queue is filled.');", + "assert(typeof new CircularQueue().dequeue === 'function', 'message: Your CircularQueue class should have a dequeue method.');", + "assert.deepEqual((function(){var test = new CircularQueue(); test.enqueue('a'); test.enqueue('b'); test.enqueue('c'); test.enqueue('d'); test.enqueue('e'); test.enqueue('f'); test.dequeue(); return test.printCollection(); })(), ['', 'b', 'c', 'd', 'e'], 'message: Your dequeue method should read the item at the current read pointer position and remove it from the queue.');", + "assert((function(){ var test = new CircularQueue(); test.enqueue('a'); test.enqueue('b'); return test.dequeue() === 'a' && test.dequeue() === 'b'; })(), 'message: Your dequeue method should return the value being removed.');", + "assert(typeof new CircularQueue().front === 'function', 'message: Your CircularQueue class should have a front method.');", + "assert((function(){ var test = new CircularQueue(); test.enqueue('a'); test.enqueue('b'); return test.front() === 'a' && test.front() === 'a'; })(), 'message: Your front method should return the item at the current read pointer position, without removing it.');" ], "type": "waypoint", - "solutions": [], + "solutions": [ + "class CircularQueue {\n constructor () {\n this._cq = ['', '', '', '', ''];\n this._reader = 0;\n this._writer = 0;\n }\n \n printCollection () {\n return this._cq;\n }\n \n _updatePointer (id) {\n if (++this[id] === this._cq.length) {\n this[id] = 0;\n }\n }\n \n enqueue (item) {\n if (Array.isArray(item)) {\n for (const x of item) {\n this.enqueue(x);\n }\n\n return;\n }\n \n this._cq[this._writer] = item;\n \n this._updatePointer('_writer');\n }\n \n dequeue () {\n const item = this._cq[this._reader];\n this._cq[this._reader] = '';\n \n this._updatePointer('_reader');\n return item;\n }\n \n front () {\n return this._cq[this._reader];\n }\n}" + ], "challengeType": 1, "translations": {} },