/interview-prep

Interview Prep material. Includes problems from Leetcode, CtCI, and elsewhere.

Primary LanguageJavaScript

Leetcode Problems

Hard

Problem Tags
895. Maximum Frequency Stack #hashmap #stack

Medium

Problem Tags
11. Container With Most Water #pointers
12. Integer to Roman #map #math
15. 3Sum #array #hashmap #pointers #set #sort
29. Divide Two Integers #bitmanip
46. Permutations #aa #recursion
54. Spiral Matrix #aa #array #matrix
71. Simplify Path #stack
74. Search a 2D Matrix #aa #array #binarysearch #matrix
105. Construct Binary Tree from Preorder and Inorder Traversal #aa #binarytree
138. Copy List with Random Pointer #linkedlist
139. Word Break #dp
189. Rotate Array #aa #array
199. Binary Tree Right Side View #dfs #stack #tree
207. Course Schedule #aa #backtracking #dfs #map #set
240. Search a 2D Matrix II #array #matrix
284. Peeking Iterator #design
322. Coin Change #aa #dp #memo #tab
413. Arithmetic Slices #math
450. Delete Node in a BST #aa #bst
478. Generate Random Point in a Circle #math
524. Longest Word in Dictionary through Deleting #pointers
535. Encode and Decode TinyURL #design #map #math
538. Convert BST to Greater Tree #bst #stack
560. Subarray Sum Equals K #array #hashmap
581. Shortest Unsorted Continuous Subarray #array
623. Add One Row to Tree #binarytree
669. Trim a Binary Search Tree #bst #tree
784. Letter Case Permutation #bottomup
785. Is Graph Bipartite? #bfs #graph #queue
820. Short Encoding of Words #trie
823. Binary Trees With Factors #binarytree #dp #map
841. Keys and Rooms #dfs #graph #set
856. Score of Parentheses #stack
912. Sort an Array #sort
946. Validate Stack Sequences #greedy #stack
991. Broken Calculator #greedy
1091. Shortest Path in Binary Matrix #bfs #queue
1201. Ugly Number III #binarysearch #math
1249. Minimum Remove to Make Valid Parentheses #stack
1461. Check If a String Contains All Binary Codes of Size K #string
1721. Swapping Nodes in a Linked List #linkedlist

Easy

Problem Tags
1. Two Sum #aa #array #hashmap
7. Reverse Integer #math
9. Palindrome Number #math #string
13. Roman to Integer #map #set
53. Maximum Subarray #array
108. Convert Sorted Array to Binary Search Tree #aa #array #bst #sort
110. Balanced Binary Tree #aa #binarytree #recursion #tree
118. Pascal's Triangle #aa #array
141. Linked List Cycle #linkedlist #pointers
160. Intersection of Two Linked Lists #linkedlist #pointers
167. Two Sum II - Input array is sorted #array #binarysearch #pointers
191. Number of 1 Bits #bitmanip
204. Count Primes #aa #array #math
242. Valid Anagram #hashtable #string
268. Missing Number #array #math
349. Intersection of Two Arrays #aa #array #binarysearch #set #sort
412. Fizz Buzz #aa
575. Distribute Candies #set
594. Longest Harmonious Subsequence #hashmap
637. Average of Levels in Binary Tree #bfs #binarytree #queue
645. Set Mismatch #array #math #set
706. Design HashMap #map
821. Shortest Distance to a Character #string
1332. Remove Palindromic Subsequences #string
1337. The K Weakest Rows in a Matrix #array #sort
1342. Number of Steps to Reduce a Number to Zero #bitmanip
1380. Lucky Numbers in a Matrix #aa #array #matrix

LeetCoding Challenge 2021

# February March
1. 191. Number of 1 Bits 575. Distribute Candies
2. 669. Trim a Binary Search Tree 645. Set Mismatch
3. 141. Linked List Cycle 268. Missing Number
4. 594. Longest Harmonious Subsequence 160. Intersection of Two Linked Lists
5. 71. Simplify Path 637. Average of Levels in Binary Tree
6. 199. Binary Tree Right Side View 820. Short Encoding of Words
7. 821. Shortest Distance to a Character 706. Design HashMap
8. 284. Peeking Iterator 1332. Remove Palindromic Subsequences
9. 538. Convert BST to Greater Tree 623. Add One Row to Tree
10. 138. Copy List with Random Pointer 12. Integer to Roman
11. 242. Valid Anagram 322. Coin Change
12. 1342. Number of Steps to Reduce a Number to Zero 1461. Check If a String Contains All Binary Codes of Size K
13. 1091. Shortest Path in Binary Matrix 823. Binary Trees With Factors
14. 785. Is Graph Bipartite? 1721. Swapping Nodes in a Linked List
15. 1337. The K Weakest Rows in a Matrix 535. Encode and Decode TinyURL
16. 784. Letter Case Permutation
17. 11. Container With Most Water 478. Generate Random Point in a Circle
18. 413. Arithmetic Slices
19. 1249. Minimum Remove to Make Valid Parentheses 841. Keys and Rooms
20. 13. Roman to Integer
21. 991. Broken Calculator
22. 524. Longest Word in Dictionary through Deleting
23. 240. Search a 2D Matrix II
24. 856. Score of Parentheses
25. 581. Shortest Unsorted Continuous Subarray
26. 946. Validate Stack Sequences
27. 29. Divide Two Integers
28. 895. Maximum Frequency Stack

Notes

// Get # of digits in a number.
function numDigits(num) {
    if (num === 0) return 1;
    return Math.floor(Math.log10(Math.abs(num))) + 1;
}

// Get nth digit from right in a number.
function getNth(num, n) {
    return Math.floor((num / Math.pow(10, n - 1)) % 10);
}

// Gauss Triangular Number - Like factorial but with addition instead of multiplication.
// triangle(5) => 15 (5 + 4 + 3 + 2 + 1)
function triangle(num) {
    return (num ** 2 + num) / 2;
}

// Least Common Multiple - The smallest number which can be divided by two given numbers.
function lcm(a, b) {
    return a * b / gcd(a, b);
}

// Greatest Common Divisor - The biggest number which can divide evenly into both the given numbers.
function gcd(a, b) {
    while (a % b > 0)
        [b, a] = [a % b, b];
    return b;
}

// Function - Returns count of numbers divisible by a, b, or c under a given n number.
// n(AuBuC) = n(A) + n(B) + n(C) - n(A∩B) - n(A∩C) - n(B∩C) + n(A∩B∩C)
function unionCountDivisible(n, a, b, c) {
    const ab = lcm(a, b), bc = lcm(b, c), ac = lcm(a, c), abc = lcm(ab, c);
    return Math.floor(n / a) + Math.floor(n / b) + Math.floor(n / c) - Math.floor(n / ab) - Math.floor(n / bc) - Math.floor(n / ac) + (Math.floor(n / abc));
}

// Use Dynamic Programming (DP, Memoization, Tabulation) when
// 1. There are overlapping subproblems
// 2. An optimal substructure