Intermediate JavaScript Problems 101–200
State the invariant, choose a suitable data structure, preserve cancellation and failure behavior, and justify every complexity claim.
101. Balanced Parentheses
Difficulty: Intermediate · Category: Stacks
Problem statement: Validate balanced parentheses, brackets and braces.
Input: string
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: ([{}]) → true
Hint: Identify the invariant or transformation first; push opening tokens and match each closer.
Recommended approach: Push opening tokens and match each closer.
Complexity: O(n) time and O(n) space.
Solution explanation
Push opening tokens and match each closer. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
102. Minimum Bracket Removals
Difficulty: Intermediate · Category: Stacks
Problem statement: Return the minimum count of removals needed to balance parentheses.
Input: string
Output: integer
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: ())( → 2
Hint: Identify the invariant or transformation first; track unmatched opens and invalid closes.
Recommended approach: Track unmatched opens and invalid closes.
Complexity: O(n) time and O(1) space.
Solution explanation
Track unmatched opens and invalid closes. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
103. Evaluate Postfix Expression
Difficulty: Intermediate · Category: Stacks
Problem statement: Evaluate a space-delimited postfix arithmetic expression.
Input: token string
Output: number
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 2 3 4 * + → 14
Hint: Identify the invariant or transformation first; push operands and apply operators to the last two.
Recommended approach: Push operands and apply operators to the last two.
Complexity: O(n) time and O(n) space.
Solution explanation
Push operands and apply operators to the last two. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
104. Queue with Two Stacks
Difficulty: Intermediate · Category: Queues
Problem statement: Implement FIFO enqueue and dequeue using two stacks.
Input: operation sequence
Output: results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: enqueue 1,2 then dequeue → 1
Hint: Identify the invariant or transformation first; transfer only when output stack is empty.
Recommended approach: Transfer only when output stack is empty.
Complexity: Amortized O(1) per operation and O(n) space.
Solution explanation
Transfer only when output stack is empty. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match Amortized O(1) per operation and O(n) space.
105. Circular Buffer
Difficulty: Intermediate · Category: Queues
Problem statement: Implement a fixed-capacity queue with wraparound indices.
Input: capacity and operations
Output: queue behavior
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: capacity 3 enqueues → bounded order
Hint: Identify the invariant or transformation first; track head, size and modulo index.
Recommended approach: Track head, size and modulo index.
Complexity: O(1) per operation and O(k) space.
Solution explanation
Track head, size and modulo index. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per operation and O(k) space.
106. Singly Linked List
Difficulty: Intermediate · Category: Linked Lists
Problem statement: Implement append, prepend, find and remove by value.
Input: operation sequence
Output: list state
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: append 1,2 remove 1 → [2]
Hint: Identify the invariant or transformation first; maintain head, tail and size invariants.
Recommended approach: Maintain head, tail and size invariants.
Complexity: O(1) ends and O(n) search or removal.
Solution explanation
Maintain head, tail and size invariants. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) ends and O(n) search or removal.
107. Reverse Linked List
Difficulty: Intermediate · Category: Linked Lists
Problem statement: Reverse a singly linked list in place.
Input: head node
Output: new head
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1→2→3 → 3→2→1
Hint: Identify the invariant or transformation first; iteratively redirect next pointers.
Recommended approach: Iteratively redirect next pointers.
Complexity: O(n) time and O(1) space.
Solution explanation
Iteratively redirect next pointers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
108. Detect Linked List Cycle
Difficulty: Intermediate · Category: Linked Lists
Problem statement: Determine whether a singly linked list contains a cycle.
Input: head node
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1→2→1 → true
Hint: Identify the invariant or transformation first; use slow and fast pointers.
Recommended approach: Use slow and fast pointers.
Complexity: O(n) time and O(1) space.
Solution explanation
Use slow and fast pointers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
109. Middle Linked List Node
Difficulty: Intermediate · Category: Linked Lists
Problem statement: Return the middle node, choosing the second for even length.
Input: head node
Output: node
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1→2→3→4 → 3
Hint: Identify the invariant or transformation first; advance fast twice and slow once.
Recommended approach: Advance fast twice and slow once.
Complexity: O(n) time and O(1) space.
Solution explanation
Advance fast twice and slow once. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
110. Merge Sorted Linked Lists
Difficulty: Intermediate · Category: Linked Lists
Problem statement: Merge two sorted linked lists by relinking nodes.
Input: two heads
Output: merged head
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1→4 and 2→3 → 1→2→3→4
Hint: Identify the invariant or transformation first; use a dummy head and two pointers.
Recommended approach: Use a dummy head and two pointers.
Complexity: O(n+m) time and O(1) auxiliary space.
Solution explanation
Use a dummy head and two pointers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(1) auxiliary space.
111. Binary Search First Occurrence
Difficulty: Intermediate · Category: Searching
Problem statement: Find the first index of a target in a sorted array with duplicates.
Input: sorted array and target
Output: index or -1
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2,2,2,3],2 → 1
Hint: Identify the invariant or transformation first; continue left after finding a match.
Recommended approach: Continue left after finding a match.
Complexity: O(log n) time and O(1) space.
Solution explanation
Continue left after finding a match. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.
112. Binary Search Insertion Point
Difficulty: Intermediate · Category: Searching
Problem statement: Return the index where a target should be inserted to keep order.
Input: sorted array and target
Output: index
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,3,5],4 → 2
Hint: Identify the invariant or transformation first; search a half-open range for first value not less than target.
Recommended approach: Search a half-open range for first value not less than target.
Complexity: O(log n) time and O(1) space.
Solution explanation
Search a half-open range for first value not less than target. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.
113. Search Rotated Array
Difficulty: Intermediate · Category: Searching
Problem statement: Find a target in a rotated sorted array with distinct values.
Input: rotated array and target
Output: index or -1
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [4,5,1,2,3],2 → 3
Hint: Identify the invariant or transformation first; at each step identify the sorted half.
Recommended approach: At each step identify the sorted half.
Complexity: O(log n) time and O(1) space.
Solution explanation
At each step identify the sorted half. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.
114. Integer Square Root
Difficulty: Intermediate · Category: Searching
Problem statement: Return floor of the square root without Math.sqrt.
Input: non-negative integer
Output: integer
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 10 → 3
Hint: Identify the invariant or transformation first; binary search candidate values using division-safe comparison.
Recommended approach: Binary search candidate values using division-safe comparison.
Complexity: O(log n) time and O(1) space.
Solution explanation
Binary search candidate values using division-safe comparison. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.
115. Peak Element
Difficulty: Intermediate · Category: Searching
Problem statement: Return an index whose value is greater than its neighbors.
Input: number array
Output: index
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,3,2] → 1
Hint: Identify the invariant or transformation first; binary search toward the rising slope.
Recommended approach: Binary search toward the rising slope.
Complexity: O(log n) time and O(1) space.
Solution explanation
Binary search toward the rising slope. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.
116. Bubble Sort Instrumented
Difficulty: Intermediate · Category: Sorting
Problem statement: Implement bubble sort and report comparison and swap counts.
Input: number array
Output: sorted array and metrics
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [3,1,2] → [1,2,3]
Hint: Identify the invariant or transformation first; stop early when a pass performs no swap.
Recommended approach: Stop early when a pass performs no swap.
Complexity: O(n²) worst time and O(1) auxiliary space.
Solution explanation
Stop early when a pass performs no swap. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n²) worst time and O(1) auxiliary space.
117. Insertion Sort Stable
Difficulty: Intermediate · Category: Sorting
Problem statement: Implement stable insertion sort on records by a key.
Input: records and key selector
Output: sorted copy
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: priorities 2,1 → 1,2
Hint: Identify the invariant or transformation first; shift larger elements right and insert current.
Recommended approach: Shift larger elements right and insert current.
Complexity: O(n²) time and O(n) copied space.
Solution explanation
Shift larger elements right and insert current. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n²) time and O(n) copied space.
118. Merge Sort
Difficulty: Intermediate · Category: Sorting
Problem statement: Implement stable merge sort without mutating input.
Input: array and comparator
Output: sorted array
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [4,1,3] → [1,3,4]
Hint: Identify the invariant or transformation first; divide recursively and merge with two pointers.
Recommended approach: Divide recursively and merge with two pointers.
Complexity: O(n log n) time and O(n) space.
Solution explanation
Divide recursively and merge with two pointers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.
119. Quickselect Kth Smallest
Difficulty: Intermediate · Category: Selection
Problem statement: Return the kth smallest value without fully sorting.
Input: array and one-based k
Output: value
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [7,2,5,1],2 → 2
Hint: Identify the invariant or transformation first; partition around a pivot and recurse only one side.
Recommended approach: Partition around a pivot and recurse only one side.
Complexity: Average O(n) time and O(log n) stack.
Solution explanation
Partition around a pivot and recurse only one side. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match Average O(n) time and O(log n) stack.
120. Top K Frequent Values
Difficulty: Intermediate · Category: Heaps
Problem statement: Return the k most frequent primitive values.
Input: array and k
Output: values
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [a,a,b,c,c,c],2 → [c,a]
Hint: Identify the invariant or transformation first; count with Map then maintain a size-k min heap.
Recommended approach: Count with Map then maintain a size-k min heap.
Complexity: O(n log k) time and O(n) space.
Solution explanation
Count with Map then maintain a size-k min heap. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log k) time and O(n) space.
121. Min Heap Complete
Difficulty: Intermediate · Category: Heaps
Problem statement: Implement push, peek and pop for a numeric min heap.
Input: operation sequence
Output: results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: push 3,1,2 pop → 1
Hint: Identify the invariant or transformation first; use array parent and child indices with sift operations.
Recommended approach: Use array parent and child indices with sift operations.
Complexity: O(log n) update, O(1) peek, O(n) space.
Solution explanation
Use array parent and child indices with sift operations. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) update, O(1) peek, O(n) space.
122. K-Way Sorted Merge
Difficulty: Intermediate · Category: Heaps
Problem statement: Merge k sorted arrays efficiently.
Input: array of sorted arrays
Output: sorted array
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [[1,4],[2,3]] → [1,2,3,4]
Hint: Identify the invariant or transformation first; heap the current value from each array.
Recommended approach: Heap the current value from each array.
Complexity: O(N log k) time and O(k) auxiliary space.
Solution explanation
Heap the current value from each array. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(N log k) time and O(k) auxiliary space.
123. Running Median
Difficulty: Intermediate · Category: Heaps
Problem statement: Return the median after each inserted number.
Input: number stream
Output: medians
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 5,2,10 → 5,3.5,5
Hint: Identify the invariant or transformation first; balance max heap lower half and min heap upper half.
Recommended approach: Balance max heap lower half and min heap upper half.
Complexity: O(n log n) time and O(n) space.
Solution explanation
Balance max heap lower half and min heap upper half. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.
124. Trie Insert Search Prefix
Difficulty: Intermediate · Category: Tries
Problem statement: Implement insert, exact search and prefix search for lowercase words.
Input: word operations
Output: booleans
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: insert cat, prefix ca → true
Hint: Identify the invariant or transformation first; store child Maps and terminal markers.
Recommended approach: Store child Maps and terminal markers.
Complexity: O(length) per operation and O(total characters) space.
Solution explanation
Store child Maps and terminal markers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(length) per operation and O(total characters) space.
125. Autocomplete from Trie
Difficulty: Intermediate · Category: Tries
Problem statement: Return up to k lexicographically ordered words for a prefix.
Input: trie prefix k
Output: suggestions
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: car with car,cart,cat → [car,cart]
Hint: Identify the invariant or transformation first; find prefix node then DFS in sorted child order.
Recommended approach: Find prefix node then DFS in sorted child order.
Complexity: O(prefix plus visited output) time.
Solution explanation
Find prefix node then DFS in sorted child order. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(prefix plus visited output) time.
126. Tree Depth First Traversals
Difficulty: Intermediate · Category: Trees
Problem statement: Return preorder, inorder and postorder arrays for a binary tree.
Input: root node
Output: three arrays
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: root 2 with 1,3 → preorder [2,1,3]
Hint: Identify the invariant or transformation first; use recursive or explicit-stack traversal.
Recommended approach: Use recursive or explicit-stack traversal.
Complexity: O(n) time and O(h) space.
Solution explanation
Use recursive or explicit-stack traversal. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(h) space.
127. Tree Breadth First Levels
Difficulty: Intermediate · Category: Trees
Problem statement: Return binary-tree values grouped by depth.
Input: root node
Output: level arrays
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 2 with 1,3 → [[2],[1,3]]
Hint: Identify the invariant or transformation first; use a queue with head index and level size.
Recommended approach: Use a queue with head index and level size.
Complexity: O(n) time and O(n) space.
Solution explanation
Use a queue with head index and level size. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
128. Binary Tree Height
Difficulty: Intermediate · Category: Trees
Problem statement: Return edge or node height using a documented convention.
Input: root node
Output: height
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: single node → 1
Hint: Identify the invariant or transformation first; recursively take one plus maximum child height.
Recommended approach: Recursively take one plus maximum child height.
Complexity: O(n) time and O(h) space.
Solution explanation
Recursively take one plus maximum child height. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(h) space.
129. Validate Binary Search Tree
Difficulty: Intermediate · Category: Trees
Problem statement: Check that every node obeys strict BST ordering.
Input: root node
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 2 with 1,3 → true
Hint: Identify the invariant or transformation first; propagate exclusive lower and upper bounds.
Recommended approach: Propagate exclusive lower and upper bounds.
Complexity: O(n) time and O(h) space.
Solution explanation
Propagate exclusive lower and upper bounds. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(h) space.
130. Lowest Common Ancestor BST
Difficulty: Intermediate · Category: Trees
Problem statement: Find the lowest common ancestor of two values in a BST.
Input: root and two values
Output: node or null
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: BST 6,2,8 for 2 and 8 → 6
Hint: Identify the invariant or transformation first; walk left or right until values split.
Recommended approach: Walk left or right until values split.
Complexity: O(h) time and O(1) space.
Solution explanation
Walk left or right until values split. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(h) time and O(1) space.
131. Serialize Binary Tree
Difficulty: Intermediate · Category: Trees
Problem statement: Serialize and deserialize a binary tree including null structure.
Input: root ↔ string
Output:
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1 with right 2 → 1,#,2,#,#
Hint: Identify the invariant or transformation first; use preorder tokens with null markers.
Recommended approach: Use preorder tokens with null markers.
Complexity: O(n) time and O(n) space.
Solution explanation
Use preorder tokens with null markers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
132. Graph Breadth First Search
Difficulty: Intermediate · Category: Graphs
Problem statement: Return shortest unweighted distance from a start node to all reachable nodes.
Input: adjacency Map and start
Output: distance Map
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: A-B-C → A0 B1 C2
Hint: Identify the invariant or transformation first; use a queue and mark on enqueue.
Recommended approach: Use a queue and mark on enqueue.
Complexity: O(V+E) time and O(V) space.
Solution explanation
Use a queue and mark on enqueue. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(V+E) time and O(V) space.
133. Graph Depth First Components
Difficulty: Intermediate · Category: Graphs
Problem statement: Return connected components of an undirected graph.
Input: adjacency Map
Output: component arrays
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: A-B and C → [[A,B],[C]]
Hint: Identify the invariant or transformation first; start DFS from every unseen node.
Recommended approach: Start DFS from every unseen node.
Complexity: O(V+E) time and O(V) space.
Solution explanation
Start DFS from every unseen node. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(V+E) time and O(V) space.
134. Detect Directed Cycle
Difficulty: Intermediate · Category: Graphs
Problem statement: Determine whether a directed graph contains a cycle.
Input: adjacency Map
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: A→B→A → true
Hint: Identify the invariant or transformation first; track unvisited, visiting and visited colors.
Recommended approach: Track unvisited, visiting and visited colors.
Complexity: O(V+E) time and O(V) space.
Solution explanation
Track unvisited, visiting and visited colors. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(V+E) time and O(V) space.
135. Topological Sort
Difficulty: Intermediate · Category: Graphs
Problem statement: Return a valid dependency order or report a cycle.
Input: directed graph
Output: order or error
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: A→B → [A,B]
Hint: Identify the invariant or transformation first; use indegrees and a zero-indegree queue.
Recommended approach: Use indegrees and a zero-indegree queue.
Complexity: O(V+E) time and O(V) space.
Solution explanation
Use indegrees and a zero-indegree queue. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(V+E) time and O(V) space.
136. Shortest Path Grid
Difficulty: Intermediate · Category: Graphs
Problem statement: Find the shortest path length through passable grid cells.
Input: grid start end
Output: distance or -1
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: open 2x2 corner to corner → 2
Hint: Identify the invariant or transformation first; bFS four-direction neighbors and mark visited.
Recommended approach: BFS four-direction neighbors and mark visited.
Complexity: O(rows·cols) time and space.
Solution explanation
BFS four-direction neighbors and mark visited. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(rows·cols) time and space.
137. Number of Islands
Difficulty: Intermediate · Category: Graphs
Problem statement: Count orthogonally connected groups of land cells.
Input: binary grid
Output: count
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [[1,0],[0,1]] → 2
Hint: Identify the invariant or transformation first; flood-fill each unseen land cell.
Recommended approach: Flood-fill each unseen land cell.
Complexity: O(rows·cols) time and space.
Solution explanation
Flood-fill each unseen land cell. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(rows·cols) time and space.
138. Union Find Connectivity
Difficulty: Intermediate · Category: Union Find
Problem statement: Implement union, find and connected with path compression and rank.
Input: operations
Output: results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: union a,b then connected → true
Hint: Identify the invariant or transformation first; maintain parent and rank Maps.
Recommended approach: Maintain parent and rank Maps.
Complexity: Near O(alpha n) amortized and O(n) space.
Solution explanation
Maintain parent and rank Maps. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match Near O(alpha n) amortized and O(n) space.
139. Kruskal Minimum Spanning Tree
Difficulty: Intermediate · Category: Graphs
Problem statement: Return MST weight for a connected weighted undirected graph.
Input: vertices and edges
Output: weight
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: triangle edges 1,2,4 → 3
Hint: Identify the invariant or transformation first; sort edges and union non-cycling endpoints.
Recommended approach: Sort edges and union non-cycling endpoints.
Complexity: O(E log E) time and O(V) space.
Solution explanation
Sort edges and union non-cycling endpoints. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(E log E) time and O(V) space.
140. Dijkstra Shortest Paths
Difficulty: Intermediate · Category: Graphs
Problem statement: Return shortest distances with non-negative edge weights.
Input: weighted adjacency and source
Output: distances
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: A-B2, A-C5, B-C1 → C3
Hint: Identify the invariant or transformation first; use a min heap and skip stale entries.
Recommended approach: Use a min heap and skip stale entries.
Complexity: O((V+E) log V) time and O(V+E) space.
Solution explanation
Use a min heap and skip stale entries. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O((V+E) log V) time and O(V+E) space.
141. Two Sum All Unique Pairs
Difficulty: Intermediate · Category: Arrays
Problem statement: Return unique value pairs that sum to a target.
Input: number array and target
Output: pair array
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2,2,3],4 → [[1,3],[2,2]]
Hint: Identify the invariant or transformation first; use counts and canonical pair ordering.
Recommended approach: Use counts and canonical pair ordering.
Complexity: O(n) average time and O(n) space.
Solution explanation
Use counts and canonical pair ordering. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) average time and O(n) space.
142. Three Sum Zero
Difficulty: Intermediate · Category: Arrays
Problem statement: Return unique triplets whose sum is zero.
Input: number array
Output: triplets
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [-1,0,1,2,-1] → [[-1,-1,2],[-1,0,1]]
Hint: Identify the invariant or transformation first; sort then use two pointers while skipping duplicates.
Recommended approach: Sort then use two pointers while skipping duplicates.
Complexity: O(n²) time and O(n) copied space.
Solution explanation
Sort then use two pointers while skipping duplicates. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n²) time and O(n) copied space.
143. Maximum Subarray
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return the maximum contiguous subarray sum and boundaries.
Input: number array
Output: sum and indices
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [-2,1,-3,4,-1,2] → 5 for [4,-1,2]
Hint: Identify the invariant or transformation first; track best ending here and global best.
Recommended approach: Track best ending here and global best.
Complexity: O(n) time and O(1) space.
Solution explanation
Track best ending here and global best. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
144. Maximum Product Subarray
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return maximum product of a contiguous subarray.
Input: number array
Output: product
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [2,3,-2,4] → 6
Hint: Identify the invariant or transformation first; track both maximum and minimum ending products.
Recommended approach: Track both maximum and minimum ending products.
Complexity: O(n) time and O(1) space.
Solution explanation
Track both maximum and minimum ending products. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
145. Longest Increasing Subsequence Length
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return the length of a strictly increasing subsequence.
Input: number array
Output: length
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [10,9,2,5,3,7,101] → 4
Hint: Identify the invariant or transformation first; maintain minimal tails with binary search.
Recommended approach: Maintain minimal tails with binary search.
Complexity: O(n log n) time and O(n) space.
Solution explanation
Maintain minimal tails with binary search. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.
146. Coin Change Minimum
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return minimum coins needed for an amount or -1.
Input: coin values and amount
Output: count
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,3,4],6 → 2
Hint: Identify the invariant or transformation first; build bottom-up best counts from zero.
Recommended approach: Build bottom-up best counts from zero.
Complexity: O(amount·coins) time and O(amount) space.
Solution explanation
Build bottom-up best counts from zero. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(amount·coins) time and O(amount) space.
147. Climbing Stairs
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Count ways to climb n steps using one or two steps.
Input: integer n
Output: ways
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 5 → 8
Hint: Identify the invariant or transformation first; keep only previous two DP values.
Recommended approach: Keep only previous two DP values.
Complexity: O(n) time and O(1) space.
Solution explanation
Keep only previous two DP values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
148. House Robber
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Maximize non-adjacent selected values.
Input: non-negative array
Output: maximum sum
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [2,7,9,3,1] → 12
Hint: Identify the invariant or transformation first; track best including or excluding current house.
Recommended approach: Track best including or excluding current house.
Complexity: O(n) time and O(1) space.
Solution explanation
Track best including or excluding current house. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
149. Edit Distance
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return minimum insertions, deletions and substitutions between strings.
Input: two strings
Output: distance
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: kitten,sitting → 3
Hint: Identify the invariant or transformation first; fill a row-by-row DP matrix.
Recommended approach: Fill a row-by-row DP matrix.
Complexity: O(nm) time and O(min(n,m)) space.
Solution explanation
Fill a row-by-row DP matrix. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(nm) time and O(min(n,m)) space.
150. Longest Common Subsequence
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Return LCS length for two strings.
Input: two strings
Output: length
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: abcde,ace → 3
Hint: Identify the invariant or transformation first; use two-dimensional or rolling-row DP.
Recommended approach: Use two-dimensional or rolling-row DP.
Complexity: O(nm) time and O(min(n,m)) space.
Solution explanation
Use two-dimensional or rolling-row DP. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(nm) time and O(min(n,m)) space.
151. Subset Sum
Difficulty: Intermediate · Category: Dynamic Programming
Problem statement: Determine whether a subset reaches a non-negative target.
Input: non-negative array and target
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [3,34,4,12,5],9 → true
Hint: Identify the invariant or transformation first; update a Boolean reachable array backward.
Recommended approach: Update a Boolean reachable array backward.
Complexity: O(n·target) time and O(target) space.
Solution explanation
Update a Boolean reachable array backward. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n·target) time and O(target) space.
152. Generate Permutations
Difficulty: Intermediate · Category: Backtracking
Problem statement: Return all permutations of distinct values.
Input: array
Output: permutations
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2] → [[1,2],[2,1]]
Hint: Identify the invariant or transformation first; choose unused values and backtrack.
Recommended approach: Choose unused values and backtrack.
Complexity: O(n·n!) time and O(n) recursion plus output.
Solution explanation
Choose unused values and backtrack. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n·n!) time and O(n) recursion plus output.
153. Generate Subsets
Difficulty: Intermediate · Category: Backtracking
Problem statement: Return the power set of distinct values.
Input: array
Output: subsets
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2] → [[],[1],[2],[1,2]]
Hint: Identify the invariant or transformation first; at each index choose include or exclude.
Recommended approach: At each index choose include or exclude.
Complexity: O(n·2^n) time and output space.
Solution explanation
At each index choose include or exclude. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n·2^n) time and output space.
154. Combination Sum
Difficulty: Intermediate · Category: Backtracking
Problem statement: Return combinations that sum to a target with reusable positive candidates.
Input: candidates and target
Output: combinations
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [2,3,6,7],7 → [[2,2,3],[7]]
Hint: Identify the invariant or transformation first; sort and recurse from current candidate index.
Recommended approach: Sort and recurse from current candidate index.
Complexity: Exponential time and O(target/min) depth.
Solution explanation
Sort and recurse from current candidate index. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match Exponential time and O(target/min) depth.
155. N Queens Count
Difficulty: Intermediate · Category: Backtracking
Problem statement: Count valid queen placements on an n by n board.
Input: integer n
Output: count
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 4 → 2
Hint: Identify the invariant or transformation first; track occupied columns and diagonals in Sets.
Recommended approach: Track occupied columns and diagonals in Sets.
Complexity: O(n!) worst time and O(n) space.
Solution explanation
Track occupied columns and diagonals in Sets. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n!) worst time and O(n) space.
156. Word Search Grid
Difficulty: Intermediate · Category: Backtracking
Problem statement: Determine whether a word can be formed by adjacent non-reused cells.
Input: grid and word
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: ABCE grid, ABCCED → true
Hint: Identify the invariant or transformation first; dFS with temporary visited marking.
Recommended approach: DFS with temporary visited marking.
Complexity: O(rows·cols·4^word) time.
Solution explanation
DFS with temporary visited marking. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(rows·cols·4^word) time.
157. Sliding Window Maximum Sum
Difficulty: Intermediate · Category: Sliding Window
Problem statement: Return the maximum sum of any fixed-size window.
Input: number array and k
Output: sum
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [2,1,5,1,3],3 → 9
Hint: Identify the invariant or transformation first; update one running window sum.
Recommended approach: Update one running window sum.
Complexity: O(n) time and O(1) space.
Solution explanation
Update one running window sum. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
158. Longest Unique Substring
Difficulty: Intermediate · Category: Sliding Window
Problem statement: Return length of longest substring without repeated code points.
Input: string
Output: length
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: abcabcbb → 3
Hint: Identify the invariant or transformation first; move left boundary past previous occurrence via Map.
Recommended approach: Move left boundary past previous occurrence via Map.
Complexity: O(n) time and O(k) space.
Solution explanation
Move left boundary past previous occurrence via Map. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.
159. Minimum Size Subarray Sum
Difficulty: Intermediate · Category: Sliding Window
Problem statement: Find the shortest positive-number subarray with sum at least target.
Input: positive array and target
Output: length or 0
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [2,3,1,2,4,3],7 → 2
Hint: Identify the invariant or transformation first; expand right and shrink while valid.
Recommended approach: Expand right and shrink while valid.
Complexity: O(n) time and O(1) space.
Solution explanation
Expand right and shrink while valid. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.
160. Anagram Windows
Difficulty: Intermediate · Category: Sliding Window
Problem statement: Return start indices where a permutation of pattern occurs in text.
Input: text and pattern
Output: indices
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: cbaebabacd,abc → [0,6]
Hint: Identify the invariant or transformation first; maintain fixed-window frequency differences.
Recommended approach: Maintain fixed-window frequency differences.
Complexity: O(n+k) time and O(alphabet) space.
Solution explanation
Maintain fixed-window frequency differences. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+k) time and O(alphabet) space.
161. Prefix Sum Range Queries
Difficulty: Intermediate · Category: Prefix Sums
Problem statement: Answer many inclusive range-sum queries.
Input: number array and queries
Output: sums
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2,3], [0,1] → 3
Hint: Identify the invariant or transformation first; build prefix sums once and subtract boundaries.
Recommended approach: Build prefix sums once and subtract boundaries.
Complexity: O(n+q) time and O(n) space.
Solution explanation
Build prefix sums once and subtract boundaries. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+q) time and O(n) space.
162. Product Except Self
Difficulty: Intermediate · Category: Prefix Products
Problem statement: Return product of all other values without division.
Input: number array
Output: product array
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [1,2,3,4] → [24,12,8,6]
Hint: Identify the invariant or transformation first; combine prefix and suffix products.
Recommended approach: Combine prefix and suffix products.
Complexity: O(n) time and O(1) auxiliary beyond output.
Solution explanation
Combine prefix and suffix products. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) auxiliary beyond output.
163. Merge Overlapping Intervals
Difficulty: Intermediate · Category: Intervals
Problem statement: Merge overlapping or touching numeric intervals.
Input: interval array
Output: merged intervals
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [[1,3],[2,6],[8,9]] → [[1,6],[8,9]]
Hint: Identify the invariant or transformation first; sort by start and extend the last output interval.
Recommended approach: Sort by start and extend the last output interval.
Complexity: O(n log n) time and O(n) output space.
Solution explanation
Sort by start and extend the last output interval. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) output space.
164. Insert Interval
Difficulty: Intermediate · Category: Intervals
Problem statement: Insert one interval into sorted non-overlapping intervals and merge.
Input: intervals and new interval
Output: intervals
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [[1,2],[5,7]],[2,6] → [[1,7]]
Hint: Identify the invariant or transformation first; append before, merge overlap, append after.
Recommended approach: Append before, merge overlap, append after.
Complexity: O(n) time and O(n) space.
Solution explanation
Append before, merge overlap, append after. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
165. Meeting Room Count
Difficulty: Intermediate · Category: Intervals
Problem statement: Return minimum rooms needed for meeting intervals.
Input: intervals
Output: room count
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: [[0,30],[5,10],[15,20]] → 2
Hint: Identify the invariant or transformation first; sort starts and ends or use a min heap.
Recommended approach: Sort starts and ends or use a min heap.
Complexity: O(n log n) time and O(n) space.
Solution explanation
Sort starts and ends or use a min heap. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.
166. Normalize API Records
Difficulty: Intermediate · Category: Data Transformation
Problem statement: Validate and convert raw API records into a stable domain shape.
Input: unknown record array
Output: valid records and issues
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: mixed rows → normalized plus errors
Hint: Identify the invariant or transformation first; parse each field explicitly and collect path issues.
Recommended approach: Parse each field explicitly and collect path issues.
Complexity: O(n) time and O(n) output space.
Solution explanation
Parse each field explicitly and collect path issues. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.
167. Group Orders by Customer
Difficulty: Intermediate · Category: Data Transformation
Problem statement: Group orders into a Map keyed by customer ID with totals.
Input: orders
Output: grouped summaries
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: two orders for c1 → one c1 summary
Hint: Identify the invariant or transformation first; accumulate mutable local summaries then expose results.
Recommended approach: Accumulate mutable local summaries then expose results.
Complexity: O(n) time and O(k) space.
Solution explanation
Accumulate mutable local summaries then expose results. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.
168. Join Records by Foreign Key
Difficulty: Intermediate · Category: Data Transformation
Problem statement: Join child records to parent records and report orphans.
Input: parents and children
Output: joined parents and orphan list
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: users plus posts → users with posts
Hint: Identify the invariant or transformation first; index parents in a Map then append children.
Recommended approach: Index parents in a Map then append children.
Complexity: O(n+m) time and O(n+m) space.
Solution explanation
Index parents in a Map then append children. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(n+m) space.
169. Deep Clone Supported Types
Difficulty: Intermediate · Category: Objects
Problem statement: Clone arrays, plain objects, Date, Map and Set with cycle support.
Input: supported value
Output: independent clone
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: cyclic object → cyclic clone
Hint: Identify the invariant or transformation first; use a WeakMap of source to clone and type dispatch.
Recommended approach: Use a WeakMap of source to clone and type dispatch.
Complexity: O(n) time and O(n) space.
Solution explanation
Use a WeakMap of source to clone and type dispatch. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
170. Deep Equal Supported Types
Difficulty: Intermediate · Category: Objects
Problem statement: Compare arrays, plain objects, Date, Map and Set with cycle handling.
Input: two supported values
Output: Boolean
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: two equal Maps → true
Hint: Identify the invariant or transformation first; track compared object pairs and recurse by type.
Recommended approach: Track compared object pairs and recurse by type.
Complexity: O(n) expected time and O(n) space.
Solution explanation
Track compared object pairs and recurse by type. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) expected time and O(n) space.
171. Immutable Set by Path
Difficulty: Intermediate · Category: Objects
Problem statement: Return a copy with one nested own-property path changed.
Input: object path value
Output: new object
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: {a:{b:1}},a.b,2 → {a:{b:2}}
Hint: Identify the invariant or transformation first; copy only containers along the path.
Recommended approach: Copy only containers along the path.
Complexity: O(depth) time and space.
Solution explanation
Copy only containers along the path. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(depth) time and space.
172. JSON Diff Patch Simple
Difficulty: Intermediate · Category: Objects
Problem statement: Produce add, remove and replace operations between JSON-compatible records.
Input: old and new JSON values
Output: operations
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: {a:1} to {a:2} → replace /a
Hint: Identify the invariant or transformation first; recursively compare types, keys and array positions.
Recommended approach: Recursively compare types, keys and array positions.
Complexity: O(n) time and O(n) space.
Solution explanation
Recursively compare types, keys and array positions. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
173. Semver Compare Basic
Difficulty: Intermediate · Category: Strings
Problem statement: Compare valid core semantic versions without prerelease metadata.
Input: two version strings
Output: -1 0 or 1
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 1.10.0 vs 1.2.0 → 1
Hint: Identify the invariant or transformation first; parse numeric components and compare left to right.
Recommended approach: Parse numeric components and compare left to right.
Complexity: O(length) time and O(1) space.
Solution explanation
Parse numeric components and compare left to right. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(length) time and O(1) space.
174. Template Interpolation Safe
Difficulty: Intermediate · Category: Strings
Problem statement: Replace named placeholders from a values record while leaving unknown keys visible.
Input: template and values
Output: string
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: Hello {name} with name Sam → Hello Sam
Hint: Identify the invariant or transformation first; use a bounded regex and own-property lookup.
Recommended approach: Use a bounded regex and own-property lookup.
Complexity: O(n) time and O(n) output space.
Solution explanation
Use a bounded regex and own-property lookup. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.
175. Markdown Heading Index
Difficulty: Intermediate · Category: Strings
Problem statement: Extract ATX headings outside fenced code blocks.
Input: markdown string
Output: heading records
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: # A then ## B → levels 1 and 2
Hint: Identify the invariant or transformation first; scan lines while toggling fence state.
Recommended approach: Scan lines while toggling fence state.
Complexity: O(n) time and O(h) space.
Solution explanation
Scan lines while toggling fence state. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(h) space.
176. CSV Parser Basic Quotes
Difficulty: Intermediate · Category: Parsing
Problem statement: Parse CSV rows supporting quoted commas and doubled quotes.
Input: CSV text
Output: rows
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: "a,b",c → [[a,b,c]]
Hint: Identify the invariant or transformation first; use a finite-state character scanner.
Recommended approach: Use a finite-state character scanner.
Complexity: O(n) time and O(n) output space.
Solution explanation
Use a finite-state character scanner. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.
177. Log Line Parser
Difficulty: Intermediate · Category: Regular Expressions
Problem statement: Parse timestamp, level and message from a defined log format.
Input: line
Output: record or error
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 2026-... INFO Ready → fields
Hint: Identify the invariant or transformation first; use an anchored named-capture pattern then validate timestamp.
Recommended approach: Use an anchored named-capture pattern then validate timestamp.
Complexity: O(n) time and O(n) output space.
Solution explanation
Use an anchored named-capture pattern then validate timestamp. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.
178. Redact Sensitive Fields
Difficulty: Intermediate · Category: Security
Problem statement: Deeply copy JSON-compatible data while masking configured key names.
Input: value and sensitive-key Set
Output: redacted copy
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: {password:x} → {password:[REDACTED]}
Hint: Identify the invariant or transformation first; traverse values and compare keys case-insensitively.
Recommended approach: Traverse values and compare keys case-insensitively.
Complexity: O(n) time and O(n) space.
Solution explanation
Traverse values and compare keys case-insensitively. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.
179. URL Allowlist Validator
Difficulty: Intermediate · Category: Security
Problem statement: Accept only HTTPS URLs from configured hostnames.
Input: candidate and allowed hosts
Output: URL or error
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: https://example.com/a → accepted
Hint: Identify the invariant or transformation first; parse with URL and compare protocol and hostname exactly.
Recommended approach: Parse with URL and compare protocol and hostname exactly.
Complexity: O(n) parse time and O(1) space.
Solution explanation
Parse with URL and compare protocol and hostname exactly. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) parse time and O(1) space.
180. Prototype Safe Merge
Difficulty: Intermediate · Category: Security
Problem statement: Merge allowlisted configuration keys without prototype pollution.
Input: defaults input schema
Output: config
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: input __proto__ → rejected
Hint: Identify the invariant or transformation first; construct output from schema keys only.
Recommended approach: Construct output from schema keys only.
Complexity: O(k) time and O(k) space.
Solution explanation
Construct output from schema keys only. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.
181. Abortable Fetch Retry
Difficulty: Intermediate · Category: Async JavaScript
Problem statement: Fetch with bounded retries, backoff, timeout and external cancellation.
Input: URL options
Output: response or error
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: two transient failures then success → response
Hint: Identify the invariant or transformation first; compose AbortSignals and retry only idempotent transient outcomes.
Recommended approach: Compose AbortSignals and retry only idempotent transient outcomes.
Complexity: O(attempts) requests and O(1) state.
Solution explanation
Compose AbortSignals and retry only idempotent transient outcomes. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(attempts) requests and O(1) state.
182. Concurrency Limited Map
Difficulty: Intermediate · Category: Concurrency
Problem statement: Map async work over items with a maximum in-flight count while preserving order.
Input: items limit async worker
Output: results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 5 items limit2 → ordered results
Hint: Identify the invariant or transformation first; run a fixed number of workers sharing the next index.
Recommended approach: Run a fixed number of workers sharing the next index.
Complexity: O(n) scheduling and O(n) results.
Solution explanation
Run a fixed number of workers sharing the next index. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) scheduling and O(n) results.
183. Promise Pool All Settled
Difficulty: Intermediate · Category: Concurrency
Problem statement: Process tasks with a limit and collect every fulfillment or rejection.
Input: task functions and limit
Output: settled results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: one fail one success → both statuses
Hint: Identify the invariant or transformation first; wrap each worker call into a settled record.
Recommended approach: Wrap each worker call into a settled record.
Complexity: O(n) work and O(n) space.
Solution explanation
Wrap each worker call into a settled record. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) work and O(n) space.
184. Async Queue Consumer
Difficulty: Intermediate · Category: Concurrency
Problem statement: Create an async iterable queue with push, close and error.
Input: producer operations
Output: for-await values
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: push a,b close → a then b
Hint: Identify the invariant or transformation first; resolve waiting consumers or buffer bounded values.
Recommended approach: Resolve waiting consumers or buffer bounded values.
Complexity: O(1) average operations and O(capacity) space.
Solution explanation
Resolve waiting consumers or buffer bounded values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) average operations and O(capacity) space.
185. Debounce with Flush Cancel
Difficulty: Intermediate · Category: Functions
Problem statement: Implement debounce preserving this/arguments with cancel and flush methods.
Input: function delay
Output: enhanced wrapper
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: calls then flush → immediate final call
Hint: Identify the invariant or transformation first; store timer, last receiver/args and pending result policy.
Recommended approach: Store timer, last receiver/args and pending result policy.
Complexity: O(1) per call and O(1) state.
Solution explanation
Store timer, last receiver/args and pending result policy. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per call and O(1) state.
186. Throttle Leading Trailing
Difficulty: Intermediate · Category: Functions
Problem statement: Implement throttle with configurable leading and trailing calls.
Input: function interval options
Output: wrapper
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: burst → first and final calls
Hint: Identify the invariant or transformation first; track last invoke time and one trailing timer.
Recommended approach: Track last invoke time and one trailing timer.
Complexity: O(1) per call and O(1) state.
Solution explanation
Track last invoke time and one trailing timer. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per call and O(1) state.
187. Memoize with Resolver LRU
Difficulty: Intermediate · Category: Caching
Problem statement: Memoize a pure function with custom key resolver and capacity.
Input: function resolver capacity
Output: memoized function
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: repeat keys → cache hits
Hint: Identify the invariant or transformation first; use Map insertion order and evict oldest.
Recommended approach: Use Map insertion order and evict oldest.
Complexity: O(1) average lookup and O(k) space.
Solution explanation
Use Map insertion order and evict oldest. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) average lookup and O(k) space.
188. TTL Cache
Difficulty: Intermediate · Category: Caching
Problem statement: Implement a cache with expiration and injectable clock.
Input: capacity ttl clock
Output: cache API
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: set then after ttl get → miss
Hint: Identify the invariant or transformation first; store expiry, lazily remove and bound size.
Recommended approach: Store expiry, lazily remove and bound size.
Complexity: O(1) average and O(k) space.
Solution explanation
Store expiry, lazily remove and bound size. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) average and O(k) space.
189. Pub Sub with Cleanup
Difficulty: Intermediate · Category: Events
Problem statement: Implement named subscriptions, once listeners and error aggregation.
Input: subscribe emit unsubscribe
Output: event results
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: once listener fires once
Hint: Identify the invariant or transformation first; snapshot listeners before synchronous dispatch.
Recommended approach: Snapshot listeners before synchronous dispatch.
Complexity: O(listeners) emit time and O(listeners) space.
Solution explanation
Snapshot listeners before synchronous dispatch. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(listeners) emit time and O(listeners) space.
190. DOM Modal Focus Trap
Difficulty: Intermediate · Category: DOM
Problem statement: Build an accessible modal with focus trapping, Escape close and focus restoration.
Input: dialog trigger content
Output: lifecycle
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: open then Escape → trigger focused
Hint: Identify the invariant or transformation first; use native dialog when possible and manage tabbable boundaries.
Recommended approach: Use native dialog when possible and manage tabbable boundaries.
Complexity: O(focusables) per cycle and O(1) state.
Solution explanation
Use native dialog when possible and manage tabbable boundaries. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(focusables) per cycle and O(1) state.
191. Virtual List Fixed Height
Difficulty: Intermediate · Category: DOM
Problem statement: Render only visible rows for a large fixed-height list.
Input: items viewport rowHeight
Output: DOM window
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: 100k items → about viewport rows
Hint: Identify the invariant or transformation first; compute start/end from scrollTop and use spacers.
Recommended approach: Compute start/end from scrollTop and use spacers.
Complexity: O(visible rows) render and O(visible rows) DOM.
Solution explanation
Compute start/end from scrollTop and use spacers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(visible rows) render and O(visible rows) DOM.
192. Infinite Scroll with Observer
Difficulty: Intermediate · Category: Browser APIs
Problem statement: Load paginated data near the list end without duplicate requests.
Input: sentinel page loader
Output: appended pages
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: intersection → next page
Hint: Identify the invariant or transformation first; use IntersectionObserver plus loading/end guards and AbortSignal.
Recommended approach: Use IntersectionObserver plus loading/end guards and AbortSignal.
Complexity: O(items loaded) time and bounded observer state.
Solution explanation
Use IntersectionObserver plus loading/end guards and AbortSignal. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(items loaded) time and bounded observer state.
193. IndexedDB Key Value Store
Difficulty: Intermediate · Category: Browser APIs
Problem statement: Wrap IndexedDB open, get, put and delete with upgrade handling.
Input: database config operations
Output: Promises
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: put a then get a → value
Hint: Identify the invariant or transformation first; promisify requests and transact per operation.
Recommended approach: Promisify requests and transact per operation.
Complexity: O(record size) work and persistent storage.
Solution explanation
Promisify requests and transact per operation. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(record size) work and persistent storage.
194. BroadcastChannel State Sync
Difficulty: Intermediate · Category: Browser APIs
Problem statement: Synchronize versioned preference updates across tabs.
Input: local updates and channel messages
Output: latest state
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: tab A theme dark → tab B dark
Hint: Identify the invariant or transformation first; include source/version and ignore stale or self messages.
Recommended approach: Include source/version and ignore stale or self messages.
Complexity: O(1) per message and O(state) storage.
Solution explanation
Include source/version and ignore stale or self messages. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per message and O(state) storage.
195. Service Worker Cache First
Difficulty: Intermediate · Category: Browser APIs
Problem statement: Implement cache-first static assets with versioned cleanup and network fallback.
Input: fetch event
Output: response
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: cached app.js → cached response
Hint: Identify the invariant or transformation first; match cache, fetch/store safe responses and delete old caches.
Recommended approach: Match cache, fetch/store safe responses and delete old caches.
Complexity: O(response size) work and cache space.
Solution explanation
Match cache, fetch/store safe responses and delete old caches. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(response size) work and cache space.
196. Accessible Tabs Component
Difficulty: Intermediate · Category: DOM
Problem statement: Build tabs with arrow-key navigation, roving tabindex and linked panels.
Input: tablist markup
Output: interactive tabs
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: ArrowRight → next tab active
Hint: Identify the invariant or transformation first; implement the WAI-ARIA tabs keyboard model.
Recommended approach: Implement the WAI-ARIA tabs keyboard model.
Complexity: O(tabs) setup and O(1) per key.
Solution explanation
Implement the WAI-ARIA tabs keyboard model. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(tabs) setup and O(1) per key.
197. Sortable Accessible Table
Difficulty: Intermediate · Category: DOM
Problem statement: Sort a table column while updating aria-sort and preserving row semantics.
Input: table and header activation
Output: reordered rows
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: activate amount → ascending
Hint: Identify the invariant or transformation first; parse cell sort keys and use stable sorting.
Recommended approach: Parse cell sort keys and use stable sorting.
Complexity: O(n log n) time and O(n) space.
Solution explanation
Parse cell sort keys and use stable sorting. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.
198. Form Async Username Check
Difficulty: Intermediate · Category: DOM
Problem statement: Validate username availability without stale responses or excessive requests.
Input: input and endpoint
Output: accessible status
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: type new value → old response ignored
Hint: Identify the invariant or transformation first; debounce, abort previous request and compare request token.
Recommended approach: Debounce, abort previous request and compare request token.
Complexity: O(requests) and O(1) active state.
Solution explanation
Debounce, abort previous request and compare request token. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(requests) and O(1) active state.
199. State Reducer with Undo
Difficulty: Intermediate · Category: State Management
Problem statement: Implement reducer history with past, present and future stacks.
Input: actions
Output: undoable state
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: edit then undo → original
Hint: Identify the invariant or transformation first; wrap a pure reducer and clear future on new action.
Recommended approach: Wrap a pure reducer and clear future on new action.
Complexity: O(1) history updates plus state-copy cost.
Solution explanation
Wrap a pure reducer and clear future on new action. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) history updates plus state-copy cost.
200. Finite Checkout Machine
Difficulty: Intermediate · Category: State Machines
Problem statement: Model cart, reviewing, submitting, success and failure transitions.
Input: state and event
Output: next state/effects
Constraints: Handle malformed and boundary inputs, avoid unnecessary mutation, and keep the solution practical for up to 100,000 input items unless stated otherwise.
Example: review plus submit → submitting
Hint: Identify the invariant or transformation first; use a transition table returning state and commands.
Recommended approach: Use a transition table returning state and commands.
Complexity: O(1) transition and O(1) state.
Solution explanation
Use a transition table returning state and commands. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) transition and O(1) state.