</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Reachability

What is this?

Reachability asks a simple question: starting where you are, can you get to the goal, and if so in how few moves? The greedy trick is to track only the farthest spot you could possibly reach so far, like keeping your eye on the edge of how far you can see rather than mapping every street. You sweep forward once, always grabbing the longest reach available, and the answer falls out.

flowchart TD A["Scan left to right"] --> B["Track farthest reachable index"] B --> C["Position past the frontier means stuck"] B --> D["Frontier covers goal means you arrive"] D --> E["Count BFS levels for fewest moves"]

๐Ÿ’ก Fun fact: Counting the minimum jumps to the end is secretly a breadth-first search in disguise. Each greedy reach is one BFS level, so you get the shortest path without ever building an actual queue of positions.

๐Ÿ”“ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: to know whether you can get somewhere โ€” or in how few moves โ€” you rarely need to explore every path. Carry the farthest position you can currently reach, extend it greedily as you scan, and the question of reachability answers itself in one pass.

The pattern

Each problem walks forward while maintaining the boundary of what is reachable. The greedy claim is that always reaching as far as possible never hurts: a longer reach can only ever help.

  • Track the farthest reach. Scan left to right keeping the maximum index you can reach from anything seen so far. If your current position ever exceeds that frontier, you are stuck; if the frontier covers the goal, you arrive.
  • Level jumps for minimum steps. When you want the fewest moves, treat reach as BFS levels: the current jump covers a window, and you only increment the step count when you exhaust that window and must commit to the next farthest reach.
  • Two-pointer subsequence cover. When you must cover a target by repeatedly consuming a source, greedily match as much of the target as possible per pass with two pointers, counting how many passes it takes.

The common thread: a frontier you push outward greedily, never second-guessing a reach that only widens your options.

The problems

Jump Game โ€” from each index you may jump up to its value. Track the farthest reachable index as you scan; if you ever stand beyond it you cannot proceed, and if the frontier reaches the last index you can finish.

Jump Game II โ€” now count the minimum jumps. Treat each jump as a BFS level covering a window of indices; extend the next reachable boundary across the window, and increment the step count only when you cross the current boundary's edge.

Shortest Way to Form String โ€” build the target as a sequence of subsequences of the source. With two pointers, scan the source once per pass consuming as much of the target as it can; the number of passes is the answer, and an unmatchable character means it is impossible.

Key takeaways

  1. Reachability is a frontier problem โ€” track the farthest point you can reach and extend it greedily.
  2. If your position ever passes the frontier, you are stuck; if the frontier covers the goal, you arrive.
  3. For minimum moves, treat reach as BFS levels and only count a step when you exhaust the current window.
  4. Greedy works here because a longer reach can never hurt โ€” it only adds options.
  5. Two-pointer greedy matching covers a target with the fewest subsequence passes.

Core idea: To build target from copies of source, sweep target once and, on each pass over source, greedily consume as many of the remaining target characters as that single pass can match. Every pass that makes progress costs exactly one copy โ€” so the minimum number of copies is just the number of passes you needed.

Problem, rephrased

Picture a label printer that can only stamp one fixed ribbon of letters โ€” your source string โ€” left to right. Each time you run the ribbon through, you may pick out some of its letters in order (skipping the rest) to build up a longer message. You're not allowed to go backwards on a single pass; once you've moved past a position on the ribbon, you can only reuse it by starting a brand-new pass.

You want to spell out a target message. How many ribbon passes is the fewest that, concatenated, spell target exactly? And if target contains a letter the ribbon doesn't even have, it's impossible โ€” return -1.

Formally (LeetCode 1055): given two strings source and target, return the minimum number of subsequences of source whose concatenation equals target. If forming target is impossible (some character of target never appears in source), return -1.

source target Output Why
"abc" "abcbc" 2 "abc" + "bc" โ€” two subsequences of "abc"
"abc" "acdbc" -1 'd' is not in "abc", so it can never be matched
"xyz" "xzyxz" 3 "xz" + "yx" + "z" โ€” three passes needed

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.