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
24 changes: 21 additions & 3 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
const Stack = require("./stack")

/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
*/
const balanced = (str) => {
throw new Error("This method has not been implemented!");
var symbols = {
"{":"}",
"[":"]",
"(":")",
}

const stack = new Stack();
const closings = Object.values(symbols);
if (str.length % 2 !== 0) { return false }
for (let i = 0; i < str.length; i++) {
if (str[i] in symbols) { // push to stack since it's a key
stack.push(str[i])
} else if (closings.includes(str[i])) {
if (symbols[stack.pop()] !== str[i]) { return false }
}
}
return stack.isEmpty()
}

/*
Expand Down
24 changes: 16 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
constructor(size = 20) {
this.store = new Array(size);
this.head = 0;
this.contains = 0;
}

enqueue(element) {
throw new Error("This method has not been implemented!");
if (this.contains === 20) { throw new Error("The queue is full"); }
this.store[(this.head+this.contains)%this.store.length] = element;
this.contains++;
}

dequeue() {
throw new Error("This method has not been implemented!");
if (this.contains === 0) { throw new Error("The queue is empty"); }
let element = this.store[this.head];
this.store[this.head] = null;
this.head = ((this.head % this.store.length) + 1) % this.contains;
this.contains--;
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!");
return this.contains
}

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

toString() {
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(element) {
this.store.addFirst(element);
}

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

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

toString() {
Expand Down
25 changes: 25 additions & 0 deletions test/queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,29 @@ 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('throws error for adding to full queue', () => {
const q = new Queue();
for (let i = 0; i < 20; i++) {
q.enqueue(i);
}
expect(q.size()).toEqual(20)
expect(() => {
q.enqueue(100)
}).toThrow("The queue is full");
});

it('is a circular queue', () => {
const q = new Queue();
for (let i = 0; i < 20; i++) {
q.enqueue(i);
}
expect(q.size()).toEqual(20)
q.dequeue()
q.dequeue()
expect(q.size()).toEqual(18)
let i = q.dequeue()
expect(i).toEqual(2)
q.enqueue(100)
expect(q.store[0]).toEqual(100)
});
});