InterviewLegend

Graphs, Grids & Topological Sort

Alex Chen32 min readUpdated 835 real interview reports

Four of these problems trace the same move: stand on a node, look at its neighbors, and walk outward until you have seen everything you can reach. A grid is a graph. Each cell is a node wired to the four cells beside it, so a Premium question-counting and a Premium problems are traversals with the edges left implicit. A Premium question and word transformation hand you the graph more openly, one as a set of prerequisites and the other as a web of one-letter edits. Learn the walk once and all four fall to it.

The walk comes in two temperaments. Breadth-first fans out in rings, nearest nodes first, which is why it answers any shortest-path question on an equal-weight graph without ever comparing distances. Depth-first plunges down one branch to its end before backing up, which suits flood-fill and cycle-hunting. The same visited discipline sits under both: mark a node the moment you queue it, never when you leave it, or the same cell rejoins the frontier and the count doubles. Topological sort is the one that looks different, and it is really a breadth-first walk over a dependency graph, releasing a task only once every task it waits on has gone.

The Walmart angle
The traversal that shows up most naturally in a Walmart retail scenario is the dependency order. An order moving through fulfillment is a chain of stages: reserve the stock, pick it, pack it, print the label, hand it to the carrier. Each stage waits on the one before it, and a build or deployment pipeline has the same shape. Topological sort is what turns a tangle of "this waits on that" into a runnable sequence, and the cycle check is not academic. A cycle means two stages each wait on the other, which is a deadlock nothing will ever clear. When an interviewer frames a problem as steps with prerequisites, or tasks with dependencies, reach for Kahn's algorithm and say why an indegree hitting zero is the moment a task becomes safe to run.
What's inside
  • 4 problems, worked step by step
  • about 32 min

Premium lessonWalmart interview guide

These took digging.

What candidates walked out with, from 20,000+ reports, forum threads behind logins and write-ups in Chinese. Then solved step by step.

100 companies. Checked every month.

Unlock all 100 companies

$49 a year · less than $1 a week · Cancel anytime

Had your Walmart interview?Tell us what they asked. Reviewed before it goes in, never with your name.
Walmart interview guide

Know what Walmart asks before you walk in.

24 coding · 4 system design · 20 behavioral questions

Get Premium$49 a year · Cancel anytime
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.