Interview
DSA for frontend engineers
Pattern-first problems with brute force, optimized approaches, complexity, and company tags. JavaScript and TypeScript solutions.
Categories
- Arrays
- Strings
- HashMaps
- Linked List
- Stack
- Queue
- Heap
- Tree
- Trie
- Graph
- Binary Search
- Sliding Window
- Dynamic Programming
- Greedy
- Backtracking
Problems
Two Sum
beginnerThe classic warm-up: find two indices that add to a target — brute force, hash map, and the follow-ups interviewers love.
beginner · Google · Amazon · Meta
Contains Duplicate
beginnerDetect any duplicate in an array — Set vs sort, O(n) hash, and follow-ups like nearby duplicates (k-window).
beginner · Google · Meta
Valid Anagram
beginnerSame characters with same counts — frequency array or sort both strings, O(n) or O(n log n).
beginner · Google · Meta · Amazon
Group Anagrams
intermediateBucket strings by sorted signature or char counts — hash map of anagram groups, complexity, and interview variants.
intermediate · Google · Meta · Amazon
Top K Frequent Elements
intermediatek most frequent numbers — count with a map, then bucket sort by frequency for average O(n).
intermediate · Google · Meta · Amazon
Product of Array Except Self
intermediateOutput[i] = product of all nums except i — prefix/suffix passes, O(n) time without division.
intermediate · Google · Meta · Amazon
Longest Consecutive Sequence
intermediateLongest run of consecutive integers in unsorted array — Set + only start at run heads for O(n) average.
intermediate · Google · Meta · Amazon
Valid Palindrome
beginnerAlphanumeric palindrome check — two pointers skipping non-alphanumerics, O(n) time O(1) space.
beginner · Google · Meta · Amazon
3Sum
intermediateFind all unique triplets that sum to zero — sort + two pointers, deduping strategy, and complexity tradeoffs.
intermediate · Google · Meta · Amazon
Container With Most Water
intermediateTwo pointers on heights for max water area — why you move the shorter side, brute O(n²) vs O(n), and edge cases.
intermediate · Google · Meta · Amazon
Best Time to Buy and Sell Stock
beginnerOne buy, one sell — track min price so far and max profit in a single O(n) pass. The classic stock DP warm-up.
beginner · Google · Meta · Amazon
Longest Substring Without Repeating
intermediateFind the longest substring with all unique characters — sliding window with a last-seen map, O(n) time.
intermediate · Google · Meta · Amazon
Longest Repeating Character Replacement
intermediateLongest substring replaceable into one character with ≤ k swaps — sliding window, max-frequency tracking, O(n).
intermediate · Google · Meta
Minimum Window Substring
advancedSmallest window in s covering all of t — sliding window with need/have counts, O(|s| + |t|).
advanced · Google · Meta
Min Stack
intermediateStack with O(1) push, pop, top, and getMin — pair each value with the min so far, or dual stacks.
intermediate · Google · Meta
Evaluate Reverse Polish Notation
intermediateEvaluate RPN with a stack — operands push, operators pop-two apply, integer division toward zero in JS.
intermediate · Google · Meta · Amazon
Valid Parentheses
beginnerCheck if brackets are valid with a stack — matching pairs, edge cases, and the interview follow-ups.
beginner · Google · Amazon · Meta
Daily Temperatures
intermediateDays until a warmer temperature — monotonic decreasing stack of indices, O(n) next-greater-element pattern.
intermediate · Google · Meta
Car Fleet
intermediateCars race to a target — sort by position, compare time-to-target on a stack, count fleets that never catch each other.
intermediate · Google · Meta
Binary Search
beginnerSearch a sorted array in O(log n) — classic template, boundary bugs, and the first-true / last-true variants.
beginner · Google · Meta · Amazon
Search in Rotated Sorted Array
intermediateFind target in a rotated sorted array of distinct ints — modified binary search on the sorted half.
intermediate · Google · Meta
Find Minimum in Rotated Sorted Array
intermediateRotated sorted array min via binary search — compare mid to hi, no-duplicates case, and the with-duplicates follow-up.
intermediate · Google · Meta
Time Based Key Value Store
intermediateset(key,value,timestamp) and get previous value — map of sorted timestamps, binary search on get.
intermediate · Google · Meta · Amazon
Reverse Linked List
beginnerReverse a singly linked list in place — iterative three-pointer walk, recursive version, and the interview follow-ups.
beginner · Google · Meta · Amazon
Merge Two Sorted Lists
beginnerMerge two sorted linked lists into one sorted list — dummy head, two pointers, O(n+m) time.
beginner · Google · Meta · Amazon
Linked List Cycle
beginnerDetect a cycle in a linked list — Set of nodes vs Floyd tortoise and hare O(1) space, plus find-entrance follow-up.
beginner · Google · Meta · Amazon
Reorder List
intermediateL0→Ln→L1→Ln-1… — find mid, reverse second half, merge alternating, O(n) time O(1) space.
intermediate · Google · Meta
Remove Nth Node From End
intermediateDelete the nth node from the end of a list in one pass — two pointers with n-gap, dummy head.
intermediate · Google · Meta
Copy List with Random Pointer
intermediateDeep-copy a linked list with next and random pointers — hash map O(n) space vs interleaving nodes O(1) extra space.
intermediate · Google · Meta · Amazon
Add Two Numbers Linked List
intermediateAdd two numbers stored as reverse linked lists — digit-by-digit with carry, unequal lengths, and the final carry edge case.
intermediate · Google · Meta
Find Duplicate Number
intermediateArray of n+1 ints in 1..n with one duplicate — Floyd cycle detection O(1) space, binary search on counts alternative.
intermediate · Google · Meta
LRU Cache
advancedDesign an LRU cache with O(1) get and put — Map + doubly linked list, Map insertion-order approach, and interview tradeoffs.
advanced · Google · Meta · Amazon
Implement Trie
intermediatePrefix tree with insert, search, startsWith — node children map, end flag, and complexity for autocomplete-style use.
intermediate · Google · Meta · Amazon
Design Add and Search Words
intermediateWord dictionary with '.' wildcards — trie insert, recursive search over branches, and complexity tradeoffs.
intermediate · Google · Meta
Word Search II
advancedFind all dictionary words on a board — Trie of words + DFS from each cell, prune dead branches.
advanced · Google · Meta · Amazon
Number of Islands
intermediateCount islands in a grid with DFS or BFS flood fill — graph framing, complexity, and common grid bugs.
intermediate · Google · Meta · Amazon
Clone Graph
intermediateDeep-copy an undirected connected graph — BFS/DFS with a map from original node to clone, neighbor wiring pitfalls.
intermediate · Google · Meta · Amazon
Max Area of Island
intermediateLargest 4-connected land component in a grid — DFS/BFS flood fill that returns area, not just count.
intermediate · Google · Meta
Pacific Atlantic Water Flow
advancedCells that can reach both oceans — multi-source DFS/BFS inland from Pacific and Atlantic borders.
advanced · Google · Meta
Course Schedule
intermediateCan you finish all courses given prerequisites — cycle detection in a directed graph via DFS colors or Kahn’s BFS.
intermediate · Google · Meta
Course Schedule II
intermediateReturn a valid course order (topological sort) or empty array if a cycle exists — Kahn BFS and DFS postorder.
intermediate · Google · Meta
Redundant Connection
intermediateFind the edge that creates a cycle in a near-tree graph — Union-Find returns the last redundant edge.
intermediate · Google · Meta · Amazon
Word Ladder
advancedShortest transformation beginWord→endWord changing one letter — BFS on implicit word graph.
advanced · Google · Meta · Amazon
Climbing Stairs
beginnern steps, take 1 or 2 at a time — Fibonacci DP, bottom-up O(1) space, and the recursion trap interviewers watch for.
beginner · Google · Meta
House Robber
intermediateMax money from houses in a line without adjacent robs — DP recurrence, O(1) space roll, and circular follow-up.
intermediate · Google · Meta
House Robber II
intermediateCircular houses — max of rob linear range [0..n-2] vs [1..n-1], reusing House Robber I as a helper.
intermediate · Google · Meta
Longest Palindromic Substring
intermediateLongest palindromic substring — expand around centers O(n²), DP table alternative, and Manacher mention for O(n).
intermediate · Google · Meta · Amazon
Coin Change
intermediateFewest coins to make amount — unbounded knapsack DP, BFS alternative, and why greedy fails on arbitrary denominations.
intermediate · Google · Meta · Amazon
Word Break
intermediateCan s be segmented into dictionary words? DP boolean array, O(n² · word checks).
intermediate · Google · Meta · Amazon
Longest Increasing Subsequence
intermediateLIS length — classic O(n²) DP and O(n log n) patience sorting with binary search tails array.
intermediate · Google · Meta
Unique Paths
intermediateRobot on m×n grid moves only right/down — DP grid or combinatorial C(m+n-2, m-1).
intermediate · Google · Meta
Jump Game
intermediateCan you reach the last index — greedy farthest reach, DP alternative, and when zero cells trap you.
intermediate · Google · Meta
Jump Game II
intermediateMinimum jumps to last index — BFS layers on the array, greedy end/far windows, O(n) without real queue.
intermediate · Google · Meta
Gas Station
intermediateCircular gas stations — if total gas ≥ cost a unique start exists; one-pass track tank and reset start on negative.
intermediate · Google · Meta · Amazon
Hand of Straights
intermediateSplit hand into groups of W consecutive cards — sorted map/counting greedy, fail when a needed card is missing.
intermediate · Google · Meta · Amazon
Merge Intervals
intermediateMerge overlapping intervals — sort by start, linear scan, and the calendar/UI variants interviewers stack on top.
intermediate · Google · Meta · Amazon
Insert Interval
intermediateInsert a new interval into sorted non-overlapping intervals — scan left, merge overlap, append right. O(n).
intermediate · Google · Meta
Non-overlapping Intervals
intermediateMinimum removals so no intervals overlap — greedy by end time, keep the interval that finishes first.
intermediate · Google · Meta · Amazon
Meeting Rooms
beginnerCan one person attend all meetings? Sort by start and check adjacent overlaps — classic interval warm-up.
beginner · Google · Meta · Amazon
Meeting Rooms II
intermediateMinimum rooms for all meetings — sort starts and ends, sweep line counting concurrent intervals.
intermediate · Google · Meta · Amazon
Reverse Bits
beginnerReverse the 32 bits of an integer — shift-and-accumulate or divide-and-conquer bit swaps.
beginner · Google · Meta
Number of 1 Bits
beginnerHamming weight — count set bits with n & (n-1) Brian Kernighan, or shift-and-mask, O(set bits).
beginner · Google · Meta · Amazon
Counting Bits
intermediateCount 1-bits for every number in [0, n] — Brian Kernighan per number, and O(n) DP using i >> 1 and i & 1.
intermediate · Google · Meta · Amazon
Missing Number
beginnerFind the missing number in 0..n — XOR all indices and values, or use Gauss sum, O(n) time O(1) space.
beginner · Google · Meta · Amazon
Sum of Two Integers
intermediateAdd without + or − operators — XOR for sum bits, AND+shift for carry, loop until carry clears.
intermediate · Google · Meta · Amazon
Invert Binary Tree
beginnerMirror a binary tree — recursive swap, BFS/DFS iterative, and why this is a five-minute interview handshake.
beginner · Google · Meta
Maximum Depth of Binary Tree
beginnerHeight of a binary tree via DFS recursion or BFS levels — the tree warm-up every interviewer trusts.
beginner · Google · Meta · Amazon
Same Tree
beginnerCheck if two binary trees are identical in structure and values — recursive DFS or BFS pair walk.
beginner · Google · Meta
Subtree of Another Tree
beginnerIs subRoot identical to some subtree of root? DFS every node and same-tree check, or serialize+find.
beginner · Google · Meta · Amazon
Lowest Common Ancestor BST
intermediateFind LCA of two nodes in a BST — walk from the root using ordering, O(h) time, iterative or recursive.
intermediate · Google · Meta · Amazon
Binary Tree Level Order
intermediateBFS level-order traversal of a binary tree — queue sizing, empty levels, and the recursive DFS variant interviewers compare.
intermediate · Google · Meta · Amazon
Validate Binary Search Tree
intermediateIs the tree a valid BST? Carry (min, max) bounds down DFS, or inorder strictly increasing.
intermediate · Google · Meta
Kth Smallest in BST
intermediateKth smallest BST value via inorder — recursive count, iterative stack, and O(h) follow-up with node subtree sizes.
intermediate · Google · Meta · Amazon
Construct Tree from Preorder Inorder
advancedRebuild a binary tree from preorder and inorder arrays — root split, index map, and O(n) recursive construction.
advanced · Google · Meta
Binary Tree Max Path Sum
advancedHard tree DP: max path sum anywhere in a binary tree — gain from one child, global answer, and negative-node traps.
advanced · Google · Meta
Serialize Deserialize Binary Tree
advancedEncode a binary tree to a string and rebuild it — preorder with null markers or BFS level order.
advanced · Google · Meta
Implement Queue with Stacks
beginnerFIFO queue from two LIFO stacks — amortized O(1) pop/peek by flushing input stack to output stack on demand.
beginner · Google · Meta
Implement Stack with Queues
beginnerLIFO stack from FIFO queues — push O(n) rotate or pop O(n); single-queue rotation technique.
beginner · Google · Meta
Sliding Window Maximum
advancedMax of every window of size k — monotonic decreasing deque of indices, O(n) total.
advanced · Google · Meta · Amazon
Find Median from Data Stream
advancedRunning median with two heaps — max-heap lows + min-heap highs, balance rules, and JS heap sketch.
advanced · Google · Meta · Amazon
K Closest Points to Origin
intermediateK nearest points to (0,0) — sort O(n log n), max-heap of size k, and quickselect average O(n).
intermediate · Google · Meta · Amazon
Task Scheduler
intermediateMin time to run tasks with cooldown n — formula from max frequency, or simulate with a max-heap.
intermediate · Google · Meta · Amazon
Design Twitter Lite
advancedpostTweet, follow/unfollow, getNewsFeed — hash maps of tweets and follows, merge k recent feeds for top 10.
advanced · Google · Meta · Amazon
Encode and Decode Strings
intermediateSerialize a list of strings to one string and back — length-prefix encoding that survives empty strings and delimiters.
intermediate · Google · Meta · Amazon
Valid Sudoku
intermediateValidate a partial 9×9 board — track seen digits per row, column, and 3×3 box with sets.
intermediate · Google · Meta
Spiral Matrix
intermediateTraverse an m×n matrix in spiral order — shrink four boundaries, O(mn) time.
intermediate · Google · Meta · Amazon
Rotate Image
intermediateRotate an n×n matrix 90° clockwise in place — transpose then reverse each row (or layer peel).
intermediate · Google · Meta
Set Matrix Zeroes
intermediateIf a cell is 0, zero its row and column — mark in-place with first row/col flags, O(1) extra space.
intermediate · Google · Meta · Amazon
Word Search
intermediateDoes a word exist on a letter grid? Backtracking DFS from each cell with visited marks.
intermediate · Google · Meta · Amazon
Subsets
intermediatePower set of distinct integers — backtracking include/exclude or bit masks, 2^n subsets.
intermediate · Google · Meta · Amazon
Combination Sum
intermediateBacktracking to all unique combinations that sum to target — reuse allowed, sort + prune, and Combination Sum II contrast.
intermediate · Google · Meta
Permutations
intermediateGenerate all permutations of a distinct array — swap-based or used-mask backtracking, n! outputs.
intermediate · Google · Meta
N-Queens
advancedPlace n queens so none attack — backtracking with column and diagonal masks, return all board layouts.
advanced · Google · Meta · Amazon
Palindrome Partitioning
intermediatePartition a string so every substring is a palindrome — backtrack with expand checks, list all partitions.
intermediate · Google · Meta · Amazon
Letter Combinations Phone Number
intermediateMap digits 2-9 to letters and backtrack all strings — BFS build alternative, empty input, and pruning none needed.
intermediate · Google · Meta · Amazon