Can Reach the Exit with Teleports
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok reported this one in July 2025, and the title sounds like a graph problem. It isn't, really. The grid can be huge, so any approach that explores every cell or tries every route is dead on arrival. The process is fully deterministic: step right, teleport if you land on a start cell, stop on an obstacle, fail on a loop. It's a simulation with a visited set, and that's the whole thing. If you blank on the loop detection during the live OA, StealthCoder is the safety net sitting invisibly on your screen.
The problem
You are exploring a rectangular labyrinth with n rows and m columns. You start in the upper-left cell (0, 0), and your goal is to reach the exit in the lower-right cell (n - 1, m - 1). The labyrinth contains obstacles and one-way teleports: Each element of obstacles is [row, col]. You cannot enter an obstacle cell. If your next move would enter one, you stop and cannot reach the exit. Each element of teleports is [startRow, startCol, endRow, endCol]. Whenever you occupy a teleport's start cell, you immediately move to its end cell. Teleportation does not work backward. Every teleport start is unique, no teleport end is another teleport start, and neither endpoint is an obstacle. Any other cell is free. At every free cell, you try to move one column to the right, from (row, col) to (row, col + 1). A teleport may move you to any valid cell, including a cell in another row or an earlier column. The same movement rules then continue from the destination. Return true if this deterministic process reaches the exit. Return false if you encounter an obstacle, leave the grid before reaching the exit, or revisit a cell and therefore enter a loop. The starting cell is processed by the same rules. Reaching the exit succeeds immediately, before applying any further movement. Function canReachExit(n: int, m: int, obstacles: int[][], teleports: int[][]) → boolean Examples Example 1 n = 3 m = 4 obstacles = [] teleports = [[0,1,2,1]] return = true The path is (0,0) → (0,1) → (2,1) → (2,2) → (2,3). Entering (0,1) triggers the teleport, and the process then reaches the exit. Example 2 n = 3 m = 3 obstacles = [[2,1]] teleports = [[0,1,2,0]] return = false The teleport moves you from (0,1) to (2,0). The next cell, (2,1), is an obstacle, so movement stops before the exit. Example 3 n = 2 m = 4 obstacles = [] teleports = [[0,1,0,0]] return = false Moving right reaches (0,1), which teleports back to (0,0). Because the process repeats the same cells forever, it cannot reach the exit.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is realizing there's no branching. From every cell you have exactly one next move, so the path is a single chain, not a search tree. Don't build the n by m grid. Put obstacles in a hash set and teleports in a hash map keyed by start cell. Then walk from (0,0). At each cell: if it's the exit, return true. If it's in the visited set, return false. Mark it visited. If it's a teleport start, jump to the end cell. Then move right and check obstacle and bounds. The pitfall is order. Check the exit before applying a teleport or any move, and check the obstacle before you step in. Also encode cells as one integer or a tuple so lookups stay O(1). Each cell is visited at most once, so the walk is bounded by the cells you actually touch. If the live OA throws you off, StealthCoder can hand you the loop-safe version fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Can Reach the Exit with Teleports 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok 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.
Can Reach the Exit with Teleports FAQ
What's the actual trick in Can Reach the Exit with Teleports?+
There's no search. Every cell has exactly one next move, so you simulate a single path. Store obstacles in a set, teleports in a map, and track visited cells. Return true at the exit, false on an obstacle, an out-of-bounds move, or a revisited cell.
Why can't I just build the grid and run BFS?+
BFS implies choices, and this process has none. Building the full n by m grid also wastes memory if the dimensions are large. Sets and maps over obstacles and teleports are enough, and you only touch cells along the one path you walk.
How do I detect the infinite loop?+
Keep a visited set of cells you've processed. If you land on a cell already in it, the process repeats forever, so return false. Example 3 shows it: (0,0) goes to (0,1), which teleports back to (0,0).
What edge cases trip people up here?+
Checking the exit too late is the big one. Reaching the exit succeeds immediately, before any further movement. Also handle a teleport landing next to an obstacle, as in Example 2, and a rightward step that walks off the last column.
How should I prepare for this in 48 hours?+
Write a clean deterministic simulation with a hash set for visited states and hash maps for lookups. Test your three examples by hand. Practice encoding (row, col) as a key. Similar simulate-until-repeat problems show up often, so this pattern pays off beyond this TikTok question.