JavaScript Algorithm Questions
Strings
Implement an algorithm to determine if a string has all unique characters.
- O(n) time complexity and O(n) space complexity using helper object / Codepen
- O(n) time complexity and O(1) space complexity using bitwise operators / Codepen
- O(n log n) time complexity and O(1) space complexity using sorting / Codepen
Remove the duplicate characters in a string without using any additional buffer.
Check whether two strings are anagram of each other.
- O(n) time complexity and O(1) space complexity using sorting / Codepen
- O(n) time complexity and O(n) space complexity using helper object / Codepen
Search for a pattern in the text
- O(mn) time complexity where n = length of the text and m = length of the pattern / Codepen
- KMP algorithm: O(m + n) time complexity where n = length of the text and m = length of the pattern / Codepen
Anagram substring search
- O(n) time complexity where n = length of the text / Codepen
- O(n) time complexity where n = length of the text / Codepen
Find the smallest window in a string containing all characters of another string
Find longest common substring
- Solution using suffix arrays, O(n) time complexity / Codepen
- Solution using 2d array, O(mn) / Codepen
Find longest common subsequence
Arrays
Write code to reverse an array in place.
Given an NxN matrix, write a method to rotate it by 90 degrees.
- O(n^2) time complexity and O(1) space complexity using matrix transpose / Codepen
- O(n^2) time complexity and O(1) space complexity / Codepen
- O(n^2) time complexity and O(n^2) space complexity using additional arrays / Codepen
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
Binary search for the sorted arrays
Linked Lists
JavaScript linked list implementations
- Singly-linked list implementation - add, remove, get, toString / Codepen
- Doubly-linked list implementation - add, remove, get, toString / Codepen
Sort linked list
- Sort singly-linked list using merge sort - O(n log n) time complexity and O(n) space complexity / Codepen
Remove duplicates from unsorted linked list
-
Remove duplicates from unsorted singly-linked list using merge sort - O(n log n) time complexity and O(n) space complexity, the array order is not saved / Codepen
-
Remove duplicates from unsorted singly-linked list using beffer - O(n) time complexity and O(n) space complexity, the array order is saved / Codepen
Add 2 numbers represented as singly-linked list.
You have two numbers represented by a linked list, where each node contains a sin- gle digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE:
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
Stack
JavaScript stack implementations
- Stack implementation using array / Codepen
- Stack implementation using singly-linked-list - push, pick and isEmpty O(1), pop O(n) / Codepen
- Stack implementation using doubly-linked-list - push, pop, pick, isEmpty O(1) / Codepen
Queue
JavaScript queue implementations
Binary Tree
JavaScript binary tree implementations
- Binary tree insert using queue; inorder, preorder and postorder traversals; find a node; remove a node; find deepest node; / Codepen
Binary Search Tree
JavaScript search binary tree implementations
- Binary search tree insert; inorder, preorder and postorder traversals; find a node; remove a node; find min node; / Codepen
Trie
JavaScript trie implementations
Suffix Array
Suffix array initialization
- Suffix array initialization - time complexity O(n^2 Log n), space compexity O(n^2) / Codepen
- LCP array (Longest Common Prefix array) / Codepen