Story Problems
Ninety-four long-form problems written like real online-assessment questions, grouped by the pattern they hide.
Online tests rarely say "use a sliding window". They describe a juice stall, a lift or a delivery route, and you have to recognise the pattern underneath. Each story problem names that pattern only after you solve it, with a hint, the approach, worked examples and reference solutions in C, C++, Java and Python.
- Recognise sliding window, two pointers, stacks, heaps, binary search, greedy and DP from a story
- Translate a wordy statement into a precise function signature
- Practise under online-assessment conditions with hidden tests
Arrays, windows & two pointers
Sliding windows, two pointers, prefix sums and matrix walks hidden in everyday stories.
| 01 | Best k-Day SponsorshipFree | Fixed-size sliding window | Easy | |
| 02 | Longest Calm Stretch | Variable window (sum ≤ limit, non-negative values) | Medium | |
| 03 | Fewest Shifts to Reach the Target | Variable window (shortest with sum ≥ target) | Medium | |
| 04 | Playlist With At Most K Genres | Variable window with a frequency map | Medium | |
| 05 | Batches Under the Risk Cap | Window with a product (count subarrays) | Medium | |
| 06 | Lifeboats | Sort + two pointers (greedy pairing) | Medium | |
| 07 | Transactions That Add Up | Prefix sum + hash map | Medium | |
| 08 | Balanced Weekend Streaks | Prefix balance + first-occurrence map | Medium | |
| 09 | Longest Working Streak With Repairs | Window with at most K zeros | Medium | |
| 10 | Pick Cards From the Ends | Fixed window on the complement | Medium | |
| 11 | Group the Occupied Seats | Fixed window (window size = number of ones) | Medium | |
| 12 | Longest Hill on the Trek | Single pass / expand from a peak | Medium | |
| 13 | Zero-Sum Teams of Three | Sort + two pointers (3Sum, counting) | Medium | |
| 14 | Shortest Stretch to Re-sort | Left-right scans (boundaries) | Medium | |
| 15 | Circular Bus Route Profit | Kadane on a circular array | Hard | |
| 16 | Rotate the Seating Chart | Matrix: transpose + reverse rows | Medium | |
| 17 | Robot Vacuum Spiral | Matrix: shrinking boundaries | Medium | |
| 18 | Broken Bulbs on the Grid | Matrix: mark rows and columns first | Medium | |
| 19 | Ticket Price Lookup | Sorted matrix: staircase search | Medium | |
| 20 | Rainfall in Any Rectangle | 2-D prefix sums | Hard |
Strings
Counting, palindromes, parsing and windows over text.
| 01 | Longest Run of Distinct BadgesFree | Sliding window + last-seen index | Medium | |
| 02 | Shortest Snippet Containing All Letters | Sliding window with a 'missing' counter | Hard | |
| 03 | Repaint the Banner | Window: length − max frequency ≤ k | Medium | |
| 04 | Hidden Anagram Check | Fixed window + letter counts | Medium | |
| 05 | Every Scrambled Key | Fixed window, collect indices | Medium | |
| 06 | Almost a Palindrome Name | Two pointers from both ends | Easy | |
| 07 | Count the Mirror Segments | Expand around every centre | Medium | |
| 08 | Longest Symmetric Decoration | Expand around centre, remember best | Medium | |
| 09 | Compress the Chant | Run-length encoding | Easy | |
| 10 | Parse the Noisy Price Tag | Careful simulation (atoi) | Medium | |
| 11 | Ancient Scoreboard | Compare with the next symbol | Easy | |
| 12 | Consistent Secret Cipher | Two hash maps (one-to-one mapping) | Easy | |
| 13 | Delivery Code Pattern | Bijection between pattern letters and words | Easy | |
| 14 | Add the Big Bills | Digit-by-digit addition with carry | Easy | |
| 15 | Common Folder Path | Vertical scanning | Easy | |
| 16 | The Robot Chant | Simulation: group equal neighbours (look-and-say) | Medium | |
| 17 | Seat Label Generator | Base-26 without zero (bijective numeration) | Medium | |
| 18 | Split the Parade Into Balanced Blocks | Greedy counter | Easy | |
| 19 | Typed With Backspace | Stack (undo) | Medium | |
| 20 | Almost the Same Password | Two pointers, allow one mismatch | Medium |
Stack
Matching, monotonic stacks and undo histories.
| 01 | Days Until a Warmer DayFree | Monotonic stack (next greater element) | Medium | |
| 02 | Decode the Compressed Message | Stack of (previous text, repeat count) | Medium | |
| 03 | Stock Price Span | Monotonic stack with counts | Medium | |
| 04 | Asteroid Collision | Stack (things cancelling from the right) | Medium | |
| 05 | Biggest Banner in the Skyline | Monotonic stack (largest rectangle in a histogram) | Hard |
Priority queue
Always taking the best (or worst) item next.
| 01 | K-th Highest Score LiveFree | Min-heap of size k | Easy | |
| 02 | Cheapest Way to Join the Ropes | Min-heap: always merge the two smallest | Medium | |
| 03 | Seat Guests With No Twin Neighbours | Max-heap of counts (alternate the two most frequent) | Medium | |
| 04 | Fewest Petrol Stops | Max-heap: pick the biggest tank you passed | Hard | |
| 05 | Trending Hashtags | Heap / sort with a custom comparator | Medium |
Binary search
Searching on the answer, not just in an array.
| 01 | Slowest Reading SpeedFree | Binary search on the answer | Medium | |
| 02 | Smallest Truck for Delivery Deadline | Binary search on the answer | Medium | |
| 03 | Find the Book on a Rotated Shelf | Binary search on a rotated sorted array | Medium | |
| 04 | Earliest Day for the Bouquets | Binary search on the answer (days) | Hard |
Greedy
When the locally best choice is provably the right one.
| 01 | Hall Booking: Most EventsFree | Interval scheduling: sort by end time | Medium | |
| 02 | How Many Meeting Rooms? | Sort starts and ends separately (two pointers) | Medium | |
| 03 | Can the Frog Reach the Far Bank? | Track the farthest reachable index | Medium | |
| 04 | Petrol Pump Round Trip | Running tank + reset (circular route) | Medium | |
| 05 | Candies for the Class | Two passes (left→right, right→left) | Hard | |
| 06 | Cookies for the Children | Sort both, match smallest sufficient | Easy | |
| 07 | CPU Task Cooldown | Greedy formula on the most frequent task | Medium | |
| 08 | Delete Numbers to Halve the List | Remove the most frequent values first | Medium | |
| 09 | Fewest Laser Shots | Interval covering: sort by end | Medium | |
| 10 | Partition the Festival Banners | Last occurrence + expanding boundary | Medium | |
| 11 | Fill the Truck | Fractional knapsack: sort by value per kg | Medium | |
| 12 | Jobs With Deadlines | Sort by profit, place in the latest free slot | Hard | |
| 13 | Biggest Number From Scores | Custom sort order (a+b vs b+a) | Medium | |
| 14 | Lemonade Stand Change | Greedy change: prefer large bills | Easy | |
| 15 | Buy Low, Sell High, Many Times | Add every upward step | Easy | |
| 16 | Erase K Digits for the Smallest Number | Monotonic stack (drop larger digit before a smaller one) | Hard | |
| 17 | Two-City Interview Trip | Sort by cost difference | Medium | |
| 18 | Balance the Brackets | Running balance counter | Medium | |
| 19 | Groups of Consecutive Cards | Always start a group at the smallest remaining card | Medium | |
| 20 | Broken Calculator | Work backwards from the target | Medium |
Dynamic programming
From recursion to memoisation to a table.
| 01 | Robot on the StaircaseFree | 1-D DP · counting ways | Easy | |
| 02 | Neighbour-Alarm Robbery | 1-D DP · take / skip | Medium | |
| 03 | Toll Staircase | 1-D DP · min cost path | Medium | |
| 04 | Vending Machine Coins | 1-D DP · unbounded, min count | Medium | |
| 05 | Decode the Secret Message | 1-D DP · counting with constraints | Medium | |
| 06 | Hashtag Splitter | 1-D DP · reachability of a prefix/suffix | Medium | |
| 07 | Stack the Boxes | 1-D DP · longest increasing subsequence | Medium | |
| 08 | Frog Minimum Jumps | 1-D DP · min steps | Medium | |
| 09 | Circular Street Robbery | 1-D DP · reuse of the take/skip idea | Medium | |
| 10 | The Pick-a-Number Game | 1-D DP · convert to house robber | Medium | |
| 11 | Warehouse Robot With Obstacles | 2-D DP · grid path counting | Medium | |
| 12 | Cheapest Delivery Route | 2-D DP · min path sum | Medium | |
| 13 | Playlist Similarity | 2-D DP · longest common subsequence | Medium | |
| 14 | Autocorrect Distance | 2-D DP · edit distance | Hard | |
| 15 | Trip Backpack | 2-D DP · 0/1 knapsack | Medium | |
| 16 | Split the Treasure Equally | 2-D DP · subset sum | Medium | |
| 17 | Pyramid Descent | 2-D DP · triangle | Medium | |
| 18 | Biggest Square Plot | 2-D DP · largest square in a matrix | Hard | |
| 19 | Ways to Pay the Bill | 2-D DP · coin combinations (order does not matter) | Medium | |
| 20 | Stock Trader With Cooldown | 2-D DP · state machine (holding / not holding) | Hard |