Classify Final Robot Displacement
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive solution here is the empty string. ZipRecruiter reported this OA in September 2024, and it looks like a freebie: count U and D, compare, done. But the return value for a net-zero walk is an empty string, not "0", not null, not "S". Candidates who skim the spec return the wrong thing on the tie case and fail hidden tests. It's a simple counting problem with one trap in the output contract. If you blank on the details live, StealthCoder is the invisible safety net running on your screen.
The problem
A robot starts at vertical position 0 and follows the characters in commands from left to right. U moves the robot one step up. D moves the robot one step down. Return \"U\" if the final position is above the start, an empty string if it is at the start, or \"D\" if it is below the start. Function classifyVerticalDisplacement(commands: String) → String Examples Example 1 commands = "UUDDUDU" return = "U" Four up moves and three down moves leave the robot one step above the start. Example 2 commands = "UDDU" return = "" Two up moves and two down moves cancel, so the robot returns to its starting position. Constraints 1 <= commands.length <= 100000 commands contains only U and D.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single running counter. Walk the string once, add 1 for U, subtract 1 for D. At the end, positive returns "U", negative returns "D", zero returns an empty string. That's O(n) time and O(1) space, which easily handles 100000 characters. The pitfall is the tie case. Example 2, "UDDU", returns "" and not a letter or a number. Another slip is returning early when the counter hits zero mid-walk. Only the final position matters, not the path. You also don't need a map or any sorting. Just count. Watch your language's string literal for empty, and don't return None or null. If the output format trips you up under the clock, StealthCoder can hand you the clean version while you stay in the driver's seat during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Classify Final Robot Displacement 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as robot return to origin. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Classify Final Robot Displacement FAQ
How hard is the ZipRecruiter robot displacement question really?+
It's easy. One pass with a counter solves it. The only thing that costs people points is the return value for a net-zero walk, which must be an empty string. Read the output spec twice and you're fine.
What's the trick to classifyVerticalDisplacement?+
Keep one integer. Increment on U, decrement on D. After the loop, check the sign. Positive gives "U", negative gives "D", zero gives "". No extra data structures needed, and order of moves doesn't matter.
What should I return when the robot ends at the start?+
Return an empty string, exactly as in Example 2 with "UDDU". Not "0", not null, not a space. Hidden tests will check this case, so it's where most wrong answers come from.
Will this pass with 100000 characters?+
Yes. A single linear scan is O(n), so 100000 characters is trivial. You use constant extra memory. Don't build intermediate lists or recompute counts inside a loop, and you're well within any reasonable limit.
How do I prep for this in 48 hours?+
Don't grind this one. Write the counter solution once, test it on the two examples and a single-character input like "U". Then spend remaining time on other counting and prefix-sum style problems, since the OA may include harder ones.