Reported September 2022
Duolingobreadth first search

Shortest Route Through a Traced Maze

Reported by candidates from Duolingo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Duolingo OA. Under 2s to a working solution.
Founder's read

Duolingo reported this one in September 2022, and the title is misleading. It sounds like a maze search, but the maze is just the cells your recorded walk touched. The real job is to replay the path, build a graph out of the visited coordinates, and find the unique shortest route from (0, 0) to the final cell. BFS is the hinted pattern. If your OA invite is two days out, this is a clean one to nail. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea fits in your head.

The problem

Duo records one route through a maze as a list path whose entries are "north", "east", "south", or "west". The route begins at coordinate (0, 0).
Every cell visited by the recorded route is known to be passable. No unvisited cell may be inferred to be passable. Two known cells are connected when they are orthogonally adjacent.
The source guarantees that there is exactly one shortest route through the known cells from (0, 0) to the recorded route's final coordinate. Return that route as a list of direction strings. The result may equal path when the recorded route is already optimal.

Function
shortestTracedMazeRoute(path: String[]) → String[]

Examples
Example 1
path = ["south","east","east","south","south","west","west","east","east","south"]
return = ["south","east","east","south","south","south"]
The recorded walk visits cells that create a direct vertical connection near the end. The unique shortest known route removes the west-east detour and uses six moves instead of ten.

Constraints
1 <= path.length <= 100000.
Every entry is exactly "north", "east", "south", or "west".
The source guarantees exactly one shortest route through the visited cells from the origin to the final coordinate.
The returned route is never longer than path.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Walk the path once, tracking x and y, and store every visited coordinate in a hash set. Then run BFS from (0, 0) over only those cells, with parent pointers, until you hit the final coordinate. Rebuild the route by following parents back and convert each step to a direction string. Since the shortest route is guaranteed unique, any parent choice is fine. The pitfall is scale. With up to 100000 moves, you need O(n) with hashed coordinates, so encode (x, y) as a tuple or a single integer key. Don't infer unvisited cells as open, and don't just cancel opposite adjacent moves, because loops like a square detour need real graph search. Also watch the reverse step: parents give you the route backwards. If you freeze in the live OA, StealthCoder is the hedge that reads the problem and hands you the BFS skeleton.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Shortest Route Through a Traced Maze cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Duolingo's OA.

Duolingo reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shortest Route Through a Traced Maze FAQ

What's the trick in the Duolingo traced maze problem?+

Reduce it to a graph. Every coordinate your path visits is a node, and orthogonally adjacent visited cells are edges. Then it's plain BFS from (0, 0) to the final coordinate. The maze is never given to you directly, you build it by replaying the moves.

Why not just remove opposite moves like west followed by east?+

That only handles immediate backtracking. Detours like a loop around a block can leave the path far longer than needed, and shortcuts appear between cells visited at very different times. Adjacency in the visited set is what matters, so you need BFS.

How do I handle 100000 moves efficiently?+

Use a hash set or map keyed by coordinates, and make BFS touch each visited cell once. Total work is linear in the number of distinct cells, at most 100001. Avoid grid arrays sized by max coordinate, and avoid recursion, which can overflow the stack.

How do I return the route as direction strings?+

Store a parent for each cell during BFS. From the final cell, follow parents back to the origin, compare each pair of coordinates to get the direction, then reverse the list. Make sure your north and south signs match your movement logic when replaying the path.

How should I prepare for this in 48 hours?+

Write BFS with parent pointers on a coordinate hash set until it's automatic. Practice converting coordinate differences into direction names. Test the sample, a path with no detours, and a path that's a single move. Those edge cases catch most mistakes.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Duolingo.

OA at Duolingo?
Invisible during screen share
Get it