Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
const Stack = require('./stack');
/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n), the function has to visit every node once
but operations performed in that loop are O(1)
Space Complexity: O(n), worst case scenario the stack could have on it
every character in the string
*/
const balanced = (str) => {
Comment on lines +3 to 8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("This method has not been implemented!");
const stack = new Stack();
const pairs = { '{': '}', '[': ']', '(': ')' }

for (const char of str) {
if (pairs[char]) {
stack.push(char);
} else {
const top = stack.pop();
if (!(pairs[top] === char)) {
return false;
}
}
}

return stack.isEmpty();
}

/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n), the function has to check every character in expr,
but operations in the for loop are O(1)
Space Complexity: O(n), the controlling factor is the height of the stack,
which does not grow quickly with longer expr in a normal use case but I suppose
in a worst case scenario with some weird postfix expressions would increase in size
linear to the length of the weird expression you were using
*/
const evaluatePostfix = (expr) => {
Comment on lines +27 to 34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("This method has not been implemented!");
const stack = new Stack();

for (const char of expr) {
const current = parseInt(char);
if (Number.isInteger(current)) {
stack.push(current);
} else {
const second = stack.pop();
const first = stack.pop();

if (char === '+') {
stack.push(first + second);
} else if (char === '-') {
stack.push(first - second);
} else if (char === '*') {
stack.push(first * second);
} else if (char === '/') {
const quotient = first / second;
quotient > 0 ? stack.push(Math.floor(quotient)) : stack.push(Math.ceil(quotient))
}
}
}

return stack.pop();
}

exports.balanced = balanced;
Expand Down
39 changes: 31 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
const QUEUE_SIZE = 20;

class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new Array(QUEUE_SIZE);
this.head = 0;
this.tail = 0;
}

enqueue(element) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work, very compact

throw new Error("This method has not been implemented!");
if (this.isFull()) {
throw new Error("your queue is full");
} else {
this.store[this.tail] = element;
this.tail = (this.tail + 1) % QUEUE_SIZE;
}
}

dequeue() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Very compact! However what happens if you dequeue an empty queue?

throw new Error("This method has not been implemented!");
const element = this.store[this.head];
this.store[this.head] = null;
this.head = (this.head + 1) % QUEUE_SIZE;
return element;
}

front() {
throw new Error("This method has not been implemented!");
return this.store[this.head];
}

size() {
throw new Error("This method has not been implemented!");
let count = 0;
let pointer = this.head;

while (this.store[pointer]) {
count++;
pointer = (pointer + 1) % QUEUE_SIZE;
}

return count;
}

isEmpty() {
throw new Error("This method has not been implemented!");
return this.head === this.tail;
}

isFull() {
return this.head === (this.tail + 1) % QUEUE_SIZE;
}

toString() {
let arr;
if (this.head > this.tail) {
arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail));
} else {
arr = this.store
arr = this.store;
}
return JSON.stringify(arr.filter((v) => v !== null));
}
Expand Down
15 changes: 9 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const LinkedList = require("./linked-list");

class Stack {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new LinkedList();
}

push() {
throw new Error("This method has not been implemented!");
push(value) {
this.store.addFirst(value);
}

pop() {
throw new Error("This method has not been implemented!");
const value = this.store.getFirst();
this.store.delete(value);
return value;
}

isEmpty() {
throw new Error("This method has not been implemented!");
return this.store.isEmpty();
}

toString() {
Expand Down
36 changes: 36 additions & 0 deletions test/queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,40 @@ describe("test queue implementation", () => {
expect(q.toString()).toEqual('[40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210]');
});

it('returns number of elements in queue', () => {
const q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);

expect(q.size()).toEqual(5);
});

it('returns zero on size if no elements in queue', () => {
const q = new Queue();
q.enqueue(10);
q.dequeue();

expect(q.size()).toEqual(0);
});

it('returns the first value in the queue without removing it', () => {
const q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

expect(q.front()).toEqual(10);
expect(q.dequeue()).toEqual(10);
});

it('returns null for front if queue is empty', () => {
const q = new Queue();
q.enqueue(10);
q.dequeue();

expect(q.front()).toBeNull;
});
});