From 6855dce230067b98b1608e7718ff13bc28923fe9 Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 19:24:10 +0530 Subject: [PATCH 1/7] --update : add doubly link list --- .../DoublyLinkedList/index.js | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/_DataStructures_/DoublyLinkedList/index.js diff --git a/src/_DataStructures_/DoublyLinkedList/index.js b/src/_DataStructures_/DoublyLinkedList/index.js new file mode 100644 index 00000000..36fc3106 --- /dev/null +++ b/src/_DataStructures_/DoublyLinkedList/index.js @@ -0,0 +1,68 @@ +/* eslint-disable class-methods-use-this */ +class Node { + constructor(data, previous, next) { + this.data = data; + this.previous = previous; + this.next = next; + } +} + +class DoublyLinkedList { + constructor() { + // head -> tail + // head <- tail + this.head = new Node(null, null, null); + this.tail = new Node(null, null, null); + this.head.next = this.tail; // head next point to tail + this.tail.previous = this.head; // tail previous point to head + } + + addAtBeginning(value) { + const newNode = new Node(value, this.head, this.head.next); + this.head.next.previous = newNode; + this.head.next = newNode; + } + + addAtEnd(value) { + const newNode = new Node(value, this.tail.previous, this.tail); + this.tail.previous.next = newNode; + this.tail.previous = newNode; + } + + removeAtBeginning() { + this.remove(this.head.next); + } + + removeAtEnd() { + this.remove(this.tail.previous); + } + + remove(node) { + const previousNode = node.previous; + const nextNode = node.next; + previousNode.next = nextNode; + nextNode.previous = previousNode; + } + + length() { + let address = this.head.next; + let count = 0; + while (address !== this.tail) { + count += 1; + address = address.next; + } + return count; + } + + display() { + let address = this.head.next; + while (address !== this.tail) { + console.log(address.data); + address = address.next; + } + } +} + +module.exports = { + DoublyLinkedList, +}; From 84feda9fa9f8b37eed41b7514c6889b2c577ed89 Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 21:15:10 +0530 Subject: [PATCH 2/7] --refactor : refactor length function --- src/_DataStructures_/DoublyLinkedList/index.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/_DataStructures_/DoublyLinkedList/index.js b/src/_DataStructures_/DoublyLinkedList/index.js index 36fc3106..65f7a4ab 100644 --- a/src/_DataStructures_/DoublyLinkedList/index.js +++ b/src/_DataStructures_/DoublyLinkedList/index.js @@ -4,6 +4,7 @@ class Node { this.data = data; this.previous = previous; this.next = next; + this.length = 0; } } @@ -21,20 +22,24 @@ class DoublyLinkedList { const newNode = new Node(value, this.head, this.head.next); this.head.next.previous = newNode; this.head.next = newNode; + this.length += 1; } addAtEnd(value) { const newNode = new Node(value, this.tail.previous, this.tail); this.tail.previous.next = newNode; this.tail.previous = newNode; + this.length += 1; } removeAtBeginning() { this.remove(this.head.next); + this.length -= 1; } removeAtEnd() { this.remove(this.tail.previous); + this.length -= 1; } remove(node) { @@ -45,13 +50,7 @@ class DoublyLinkedList { } length() { - let address = this.head.next; - let count = 0; - while (address !== this.tail) { - count += 1; - address = address.next; - } - return count; + return this.length; } display() { From 53ac17abdf458390a88ee5fef34e805000e94946 Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 22:29:30 +0530 Subject: [PATCH 3/7] --fix : fix doublyLinkedList length --- src/_DataStructures_/DoublyLinkedList/index.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/_DataStructures_/DoublyLinkedList/index.js b/src/_DataStructures_/DoublyLinkedList/index.js index 65f7a4ab..522924b9 100644 --- a/src/_DataStructures_/DoublyLinkedList/index.js +++ b/src/_DataStructures_/DoublyLinkedList/index.js @@ -4,7 +4,6 @@ class Node { this.data = data; this.previous = previous; this.next = next; - this.length = 0; } } @@ -16,30 +15,31 @@ class DoublyLinkedList { this.tail = new Node(null, null, null); this.head.next = this.tail; // head next point to tail this.tail.previous = this.head; // tail previous point to head + this.size = 0; } addAtBeginning(value) { const newNode = new Node(value, this.head, this.head.next); this.head.next.previous = newNode; this.head.next = newNode; - this.length += 1; + this.size += 1; } addAtEnd(value) { const newNode = new Node(value, this.tail.previous, this.tail); this.tail.previous.next = newNode; this.tail.previous = newNode; - this.length += 1; + this.size += 1; } removeAtBeginning() { this.remove(this.head.next); - this.length -= 1; + this.size -= 1; } removeAtEnd() { this.remove(this.tail.previous); - this.length -= 1; + this.size -= 1; } remove(node) { @@ -50,7 +50,7 @@ class DoublyLinkedList { } length() { - return this.length; + return this.size; } display() { @@ -62,6 +62,4 @@ class DoublyLinkedList { } } -module.exports = { - DoublyLinkedList, -}; +module.exports = DoublyLinkedList; From 7b9dd7ead0efae8c39aa04447b7710eb2a1dbe0c Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 22:33:08 +0530 Subject: [PATCH 4/7] --update : add LRUCache data structure --- .../DoublyLinkedList/lru-cache/index.js | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/_DataStructures_/DoublyLinkedList/lru-cache/index.js diff --git a/src/_DataStructures_/DoublyLinkedList/lru-cache/index.js b/src/_DataStructures_/DoublyLinkedList/lru-cache/index.js new file mode 100644 index 00000000..d843ea59 --- /dev/null +++ b/src/_DataStructures_/DoublyLinkedList/lru-cache/index.js @@ -0,0 +1,71 @@ +/* +Least recently used (LRU) - cache implementation + +get(key) – Get the value (will always be positive) of the key if the key exists in the cache, otherwise return false. +Complexity: O(1) + +set(key, value) – Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. +Complexity: O(1) +*/ + +const DoublyLinkedList = require('../index'); + +class LRUCache { + constructor(n) { + this.size = n; + this.map = new Map(); + this.list = new DoublyLinkedList(); + } + + // this method will work in O(1) + set(key, value) { + const data = { + key, + value, + }; + if (!this.map.has(key)) { + this.list.addAtBeginning(data); + this.map.set(key, this.list.head.next); + + if (this.list.length() > this.size) { + const lastNode = this.list.tail.previous.data; + this.map.delete(lastNode.key); + this.list.removeAtEnd(); + } + } else { + this.list.remove(this.map.get(key)); + this.list.addAtBeginning(data); + this.map.set(key, this.list.head.next); + } + } + + // this method will work in O(1) + get(key) { + if (this.map.has(key)) { + const node = this.map.get(key); + const { value } = node.data; + this.list.remove(node); + this.list.addAtBeginning({ + key, + value, + }); + this.map.set(key, this.list.head.next); + } + return false; + } +} + +// const lru = new LRUCache(3); +// lru.set(1, 1); +// lru.set(2, 2); +// lru.set(3, 3); +// lru.set(4, 4); +// lru.set(5, 5); +// lru.set(2, 2); +// lru.get(5, 5); +// lru.list.display(); + + +module.exports = { + LRUCache, +}; From 261695e960902157159b9c57c243dd98977b7c49 Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 23:54:26 +0530 Subject: [PATCH 5/7] --update : move LRU cache into _Algorithms_ --- .../DoublyLinkedList => _Algorithms_}/lru-cache/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{_DataStructures_/DoublyLinkedList => _Algorithms_}/lru-cache/index.js (100%) diff --git a/src/_DataStructures_/DoublyLinkedList/lru-cache/index.js b/src/_Algorithms_/lru-cache/index.js similarity index 100% rename from src/_DataStructures_/DoublyLinkedList/lru-cache/index.js rename to src/_Algorithms_/lru-cache/index.js From aae91425bc7a6c239bbbeb762cd5e419d72b72f9 Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sat, 5 Oct 2019 23:55:52 +0530 Subject: [PATCH 6/7] --fix : fix require --- src/_Algorithms_/lru-cache/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_Algorithms_/lru-cache/index.js b/src/_Algorithms_/lru-cache/index.js index d843ea59..beb96347 100644 --- a/src/_Algorithms_/lru-cache/index.js +++ b/src/_Algorithms_/lru-cache/index.js @@ -8,7 +8,7 @@ set(key, value) – Set or insert the value if the key is not already present. W Complexity: O(1) */ -const DoublyLinkedList = require('../index'); +const DoublyLinkedList = require('../../_DataStructures_/DoublyLinkedList/index'); class LRUCache { constructor(n) { From 2db844143da8ef9b3b0f420488c437b12c30d74a Mon Sep 17 00:00:00 2001 From: ashudeshwal999 Date: Sun, 6 Oct 2019 01:05:38 +0530 Subject: [PATCH 7/7] --update : README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index c839b342..c86c931a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) +- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) ### Logical Problems - [Anagrams](src/_Problems_/anagrams) @@ -52,6 +53,11 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Binary Search](src/_Searching_/BinarySearch) +### Algorithms + +- [LRU Cache](src/_Algorithms_/lru-cache) + + ### Path Finder - [A\*](src/PathFinder/AStart)