Given an n × n elevation grid where water rises to level t at time t, find the earliest time you can swim 4-directionally from (0, 0) to (n - 1, n - 1).
Practice (82 Questions · 2 Written Solutions)
Every question connects to the concepts it uses, external practice, and a local solved mark you can update yourself.
Given a sliding tile board with one blank slot (0), return the minimum number of orthogonal tile slides required to restore the canonical sorted state, or -1 if unreachable.
Reorder an integer array by a supplied list of distinct priority values, then place the remaining values in ascending order. Frequency counts let you emit every occurrence without losing duplicates.
Given student heights in line, count how many indices do not match the expected non-decreasing sorted order.
Sort an array containing only 0s, 1s, and 2s in-place in a single pass without calling the built-in library sort.
Sort the head of a singly linked list in ascending order by growing a sorted prefix one detached node at a time.
Given an integer array sorted in non-decreasing order (including negatives), return an array of the squares of each number sorted in non-decreasing order.
Sort an integer array in ascending order in strict O(n log n) worst-case time without built-in sort functions, minimizing auxiliary allocations.
Given the head of a linked list, split at the midpoint and merge sorted halves to achieve O(n log n) time.
Count all index pairs i < j where nums[i] > 2 * nums[j] during the divide-and-conquer merge step.
Given unique athlete scores, assign Gold, Silver, Bronze, and numeric placement ranks using heap ordering or index sorting.
Return the two indices whose elements sum to target in a single O(n) pass using a complement hash map.
Given vertical line heights, move inward from the shorter wall to maximize trapped water area (right - left) * min(h[left], h[right]).
Maintain a duplicate-free sliding window [left..right] across a string to find the maximum unique substring length in O(n).
Verify that every opening bracket (, [, { is closed in strict LIFO nested order with no unmatched leftovers.
For each day, compute how many days you must wait until a warmer temperature using a decreasing monotonic index stack.
Binary-search the minimum integer eating speed k in [1..max(piles)] such that all banana piles finish within h hours.
Sort intervals by start coordinate and coalesce all overlapping [start, end] ranges into disjoint intervals.
Maintain a bounded min-heap of size k so its root always holds the k-th largest score seen so far in O(log k) per update.
Repeatedly pop the smallest head across k sorted linked lists using a k-entry min-heap in O(N log k) time.
Balance a lower-half max-heap against an upper-half min-heap to insert numbers in O(log n) and read the running median in O(1).
Implement get(key) and put(key, value) in strict O(1) time by pairing a hash map with sentinel-guarded doubly linked recency pointers.
Evict the least-frequently accessed key (breaking ties by least-recent access) in O(1) using frequency-indexed doubly linked buckets.
Implement exact membership checks, insertions, and removals with your own buckets or direct-address representation.
Pack 10-letter A/C/G/T windows into 20-bit integers and track seen vs repeated patterns in compact bitsets.
Given directed weighted signal edges from source node k, compute the minimum time for all n nodes to receive the signal (or -1 if unreachable).
Find a route from top-left to bottom-right that minimizes the maximum absolute height jump between consecutive grid cells.
Find the shortest 8-directional clear path from (0, 0) to (n - 1, n - 1) using BFS or A* with Chebyshev heuristic max(|dx|, |dy|).
Turn 4 circular combination wheels one digit at a time from "0000" to target while avoiding deadends in the minimum turns.
Return the k points closest to (0, 0) using squared Euclidean distance and a bounded size-k max-heap or Quickselect.
Build a sorted structure with randomized layers that supports search, insertion, and deletion in expected logarithmic time.
Given a tree plus one extra edge, return the edge that closes a cycle by uniting endpoints in a Disjoint-Set Union.
Compute the fewest coins needed to make up amount using bottom-up 1-D dynamic programming.
Implement insert(word), search(word), and startsWith(prefix) over a character-branching prefix tree.
Use a set to notice when an array value has already appeared.
Compare character frequencies to decide whether two strings rearrange into each other.
Design a shared signature for words with the same character counts.
Move inward from both ends while ignoring characters outside the comparison rules.
Track the cheapest earlier price and the best later sale in one pass.
Combine left and right cumulative products without division.
Count earlier prefix totals that would complete a subarray with the target sum.
Reverse next pointers while preserving a reference to the unprocessed suffix.
Build a sorted chain by repeatedly choosing the smaller available head.
Detect a repeated cycle without allocating a set of visited nodes.
Store enough information to answer the minimum query after any push or pop.
Maintain a search interval and discard the half that cannot contain the target.
Identify which half remains sorted before deciding where the target can lie.
Locate the rotation boundary by comparing the midpoint with an endpoint.
Swap child branches at every node using a traversal of the tree.
Combine subtree depths to measure the longest root-to-leaf route.
Use a queue frontier to collect values one depth at a time.
Carry valid value bounds through the tree instead of checking only immediate children.
Combine information from both subtrees to find where two target paths first meet.
Track subtree height while testing the longest path passing through each node.
Count values, then retain only the k highest-frequency candidates.
Use partition boundaries to select an order statistic without a full sort.
Count connected land regions by exploring each still-unvisited starting cell.
Keep an original-to-copy map while rebuilding nodes and their neighbor links.
Start a breadth-first wave from every initial source to model simultaneous spreading.
Decide whether a directed prerequisite graph contains a cycle.
Produce a valid ordering of a directed prerequisite graph when one exists.
Choose a minimum-cost set of connections that reaches every point.
Explore include-or-skip decisions to enumerate all subsets of distinct items.
Choose an unused element at each search depth and undo the choice on return.
Build target-sum combinations with repeated candidates while avoiding duplicate orderings.
Try neighboring letters and restore visited state when a partial path fails.
Relate the number of ways to reach a step to the two immediately preceding states.
Compare taking the current value with skipping it under a non-adjacency constraint.
Maintain the best possible tail for each increasing subsequence length.
Use two prefix lengths as the state for matching or skipping sequence elements.
Reduce balanced partitioning to whether a subset can reach half the total sum.
Compare insertion, deletion, and replacement costs across two string prefixes.
Extend prefix-tree lookup so a wildcard explores every possible next branch.
Support point updates and range totals without recomputing a full prefix array.
Track the farthest reachable index as each reachable position extends the frontier.
Count greedy frontier expansions to minimize the jumps needed to reach the end.
Keep intervals with early finish times to minimize the number discarded.
Use total balance and a running deficit to identify a possible circular starting point.
Use XOR cancellation to isolate the value whose duplicates do not pair up.
Reuse a smaller integer result to count set bits for a whole interval.
Handle exponent sign and repeatedly square the base as the exponent shrinks.
Mark composite values efficiently and count the primes below a limit.
Worked solutions and interactive examples
Swim in Rising Water
Given an n × n elevation grid where water rises to level t at time t, find the earliest time you can swim 4-directionally from (0, 0) to (n - 1, n - 1).
Sliding Tile Puzzle
Given a sliding tile board with one blank slot (0), return the minimum number of orthogonal tile slides required to restore the canonical sorted state, or -1 if unreachable.