ESC

Type to search the knowledge base.

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

beginner

The 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

beginner

Detect any duplicate in an array — Set vs sort, O(n) hash, and follow-ups like nearby duplicates (k-window).

beginner · Google · Meta

Valid Anagram

beginner

Same characters with same counts — frequency array or sort both strings, O(n) or O(n log n).

beginner · Google · Meta · Amazon

Group Anagrams

intermediate

Bucket strings by sorted signature or char counts — hash map of anagram groups, complexity, and interview variants.

intermediate · Google · Meta · Amazon

Top K Frequent Elements

intermediate

k 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

intermediate

Output[i] = product of all nums except i — prefix/suffix passes, O(n) time without division.

intermediate · Google · Meta · Amazon

Longest Consecutive Sequence

intermediate

Longest run of consecutive integers in unsorted array — Set + only start at run heads for O(n) average.

intermediate · Google · Meta · Amazon

Valid Palindrome

beginner

Alphanumeric palindrome check — two pointers skipping non-alphanumerics, O(n) time O(1) space.

beginner · Google · Meta · Amazon

3Sum

intermediate

Find all unique triplets that sum to zero — sort + two pointers, deduping strategy, and complexity tradeoffs.

intermediate · Google · Meta · Amazon

Container With Most Water

intermediate

Two 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

beginner

One 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

intermediate

Find 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

intermediate

Longest substring replaceable into one character with ≤ k swaps — sliding window, max-frequency tracking, O(n).

intermediate · Google · Meta

Minimum Window Substring

advanced

Smallest window in s covering all of t — sliding window with need/have counts, O(|s| + |t|).

advanced · Google · Meta

Min Stack

intermediate

Stack 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

intermediate

Evaluate RPN with a stack — operands push, operators pop-two apply, integer division toward zero in JS.

intermediate · Google · Meta · Amazon

Valid Parentheses

beginner

Check if brackets are valid with a stack — matching pairs, edge cases, and the interview follow-ups.

beginner · Google · Amazon · Meta

Daily Temperatures

intermediate

Days until a warmer temperature — monotonic decreasing stack of indices, O(n) next-greater-element pattern.

intermediate · Google · Meta

Car Fleet

intermediate

Cars 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

beginner

Search 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

intermediate

Find 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

intermediate

Rotated 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

intermediate

set(key,value,timestamp) and get previous value — map of sorted timestamps, binary search on get.

intermediate · Google · Meta · Amazon

Reverse Linked List

beginner

Reverse 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

beginner

Merge two sorted linked lists into one sorted list — dummy head, two pointers, O(n+m) time.

beginner · Google · Meta · Amazon

Linked List Cycle

beginner

Detect 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

intermediate

L0→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

intermediate

Delete 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

intermediate

Deep-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

intermediate

Add 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

intermediate

Array 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

advanced

Design 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

intermediate

Prefix 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

intermediate

Word dictionary with '.' wildcards — trie insert, recursive search over branches, and complexity tradeoffs.

intermediate · Google · Meta

Word Search II

advanced

Find all dictionary words on a board — Trie of words + DFS from each cell, prune dead branches.

advanced · Google · Meta · Amazon

Number of Islands

intermediate

Count islands in a grid with DFS or BFS flood fill — graph framing, complexity, and common grid bugs.

intermediate · Google · Meta · Amazon

Clone Graph

intermediate

Deep-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

intermediate

Largest 4-connected land component in a grid — DFS/BFS flood fill that returns area, not just count.

intermediate · Google · Meta

Pacific Atlantic Water Flow

advanced

Cells that can reach both oceans — multi-source DFS/BFS inland from Pacific and Atlantic borders.

advanced · Google · Meta

Course Schedule

intermediate

Can 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

intermediate

Return a valid course order (topological sort) or empty array if a cycle exists — Kahn BFS and DFS postorder.

intermediate · Google · Meta

Redundant Connection

intermediate

Find the edge that creates a cycle in a near-tree graph — Union-Find returns the last redundant edge.

intermediate · Google · Meta · Amazon

Word Ladder

advanced

Shortest transformation beginWord→endWord changing one letter — BFS on implicit word graph.

advanced · Google · Meta · Amazon

Climbing Stairs

beginner

n 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

intermediate

Max 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

intermediate

Circular 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

intermediate

Longest palindromic substring — expand around centers O(n²), DP table alternative, and Manacher mention for O(n).

intermediate · Google · Meta · Amazon

Coin Change

intermediate

Fewest coins to make amount — unbounded knapsack DP, BFS alternative, and why greedy fails on arbitrary denominations.

intermediate · Google · Meta · Amazon

Word Break

intermediate

Can s be segmented into dictionary words? DP boolean array, O(n² · word checks).

intermediate · Google · Meta · Amazon

Longest Increasing Subsequence

intermediate

LIS length — classic O(n²) DP and O(n log n) patience sorting with binary search tails array.

intermediate · Google · Meta

Unique Paths

intermediate

Robot on m×n grid moves only right/down — DP grid or combinatorial C(m+n-2, m-1).

intermediate · Google · Meta

Jump Game

intermediate

Can you reach the last index — greedy farthest reach, DP alternative, and when zero cells trap you.

intermediate · Google · Meta

Jump Game II

intermediate

Minimum jumps to last index — BFS layers on the array, greedy end/far windows, O(n) without real queue.

intermediate · Google · Meta

Gas Station

intermediate

Circular 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

intermediate

Split hand into groups of W consecutive cards — sorted map/counting greedy, fail when a needed card is missing.

intermediate · Google · Meta · Amazon

Merge Intervals

intermediate

Merge overlapping intervals — sort by start, linear scan, and the calendar/UI variants interviewers stack on top.

intermediate · Google · Meta · Amazon

Insert Interval

intermediate

Insert a new interval into sorted non-overlapping intervals — scan left, merge overlap, append right. O(n).

intermediate · Google · Meta

Non-overlapping Intervals

intermediate

Minimum removals so no intervals overlap — greedy by end time, keep the interval that finishes first.

intermediate · Google · Meta · Amazon

Meeting Rooms

beginner

Can one person attend all meetings? Sort by start and check adjacent overlaps — classic interval warm-up.

beginner · Google · Meta · Amazon

Meeting Rooms II

intermediate

Minimum rooms for all meetings — sort starts and ends, sweep line counting concurrent intervals.

intermediate · Google · Meta · Amazon

Reverse Bits

beginner

Reverse the 32 bits of an integer — shift-and-accumulate or divide-and-conquer bit swaps.

beginner · Google · Meta

Number of 1 Bits

beginner

Hamming weight — count set bits with n & (n-1) Brian Kernighan, or shift-and-mask, O(set bits).

beginner · Google · Meta · Amazon

Counting Bits

intermediate

Count 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

beginner

Find 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

intermediate

Add without + or − operators — XOR for sum bits, AND+shift for carry, loop until carry clears.

intermediate · Google · Meta · Amazon

Invert Binary Tree

beginner

Mirror 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

beginner

Height of a binary tree via DFS recursion or BFS levels — the tree warm-up every interviewer trusts.

beginner · Google · Meta · Amazon

Same Tree

beginner

Check if two binary trees are identical in structure and values — recursive DFS or BFS pair walk.

beginner · Google · Meta

Subtree of Another Tree

beginner

Is 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

intermediate

Find 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

intermediate

BFS 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

intermediate

Is the tree a valid BST? Carry (min, max) bounds down DFS, or inorder strictly increasing.

intermediate · Google · Meta

Kth Smallest in BST

intermediate

Kth 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

advanced

Rebuild 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

advanced

Hard 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

advanced

Encode a binary tree to a string and rebuild it — preorder with null markers or BFS level order.

advanced · Google · Meta

Implement Queue with Stacks

beginner

FIFO 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

beginner

LIFO stack from FIFO queues — push O(n) rotate or pop O(n); single-queue rotation technique.

beginner · Google · Meta

Sliding Window Maximum

advanced

Max of every window of size k — monotonic decreasing deque of indices, O(n) total.

advanced · Google · Meta · Amazon

Find Median from Data Stream

advanced

Running 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

intermediate

K 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

intermediate

Min time to run tasks with cooldown n — formula from max frequency, or simulate with a max-heap.

intermediate · Google · Meta · Amazon

Design Twitter Lite

advanced

postTweet, follow/unfollow, getNewsFeed — hash maps of tweets and follows, merge k recent feeds for top 10.

advanced · Google · Meta · Amazon

Encode and Decode Strings

intermediate

Serialize a list of strings to one string and back — length-prefix encoding that survives empty strings and delimiters.

intermediate · Google · Meta · Amazon

Valid Sudoku

intermediate

Validate a partial 9×9 board — track seen digits per row, column, and 3×3 box with sets.

intermediate · Google · Meta

Spiral Matrix

intermediate

Traverse an m×n matrix in spiral order — shrink four boundaries, O(mn) time.

intermediate · Google · Meta · Amazon

Rotate Image

intermediate

Rotate an n×n matrix 90° clockwise in place — transpose then reverse each row (or layer peel).

intermediate · Google · Meta

Set Matrix Zeroes

intermediate

If 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

intermediate

Does a word exist on a letter grid? Backtracking DFS from each cell with visited marks.

intermediate · Google · Meta · Amazon

Subsets

intermediate

Power set of distinct integers — backtracking include/exclude or bit masks, 2^n subsets.

intermediate · Google · Meta · Amazon

Combination Sum

intermediate

Backtracking to all unique combinations that sum to target — reuse allowed, sort + prune, and Combination Sum II contrast.

intermediate · Google · Meta

Permutations

intermediate

Generate all permutations of a distinct array — swap-based or used-mask backtracking, n! outputs.

intermediate · Google · Meta

N-Queens

advanced

Place n queens so none attack — backtracking with column and diagonal masks, return all board layouts.

advanced · Google · Meta · Amazon

Palindrome Partitioning

intermediate

Partition a string so every substring is a palindrome — backtrack with expand checks, list all partitions.

intermediate · Google · Meta · Amazon

Letter Combinations Phone Number

intermediate

Map digits 2-9 to letters and backtrack all strings — BFS build alternative, empty input, and pruning none needed.

intermediate · Google · Meta · Amazon