Reported June 2024
Ubersimulation

Find Length of Longest Common Prefix

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

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

Uber asked this in June 2024 and it's a string alignment problem dressed up as a prefix hunt. You're comparing multiple strings and finding the longest prefix they all share. The trap is thinking you need to generate or track every possible prefix, which kills your time. The real move is to iterate character by character across all strings in parallel, stopping when you hit a mismatch. It's straightforward, but the dynamic-programming hint they planted might throw you. Don't overthink it. StealthCoder will catch you if the pattern shifts mid-problem.

Pattern and pitfall

The pattern here isn't really dynamic programming, it's simulation with early exit. Start at position 0 across all input strings. Compare the character at that position in each string. If all match, move to the next position. The moment one string runs out or a character doesn't match, stop and return what you've collected so far. The DP hint is noise, probably flagging a variant where you're computing prefixes across a trie or scoring overlaps, but the canonical version is just the horizontal scan. Edge cases: empty input, single string, strings with no common prefix. If you blank on the logic, StealthCoder feeds you the template and you're protected.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find Length of Longest Common Prefix 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest common prefix. If you have time before the OA, drill that.

⏵ The honest play

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

Uber 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.

Find Length of Longest Common Prefix FAQ

Is this really dynamic programming or is that a red herring?+

It's a red herring for the standard version. The core solution is linear scan across all strings in parallel, stopping on the first mismatch. DP variants exist (like scoring prefix matches in a trie), but Uber's June OA likely tested the greedy horizontal approach. If the problem statement is ambiguous, the scan method works.

What's the trick that kills most candidates?+

Overthinking by trying to generate all prefixes, sort them, or use a hash table. Also forgetting to handle strings of unequal length or empty input. The real trick is recognizing that you compare character-by-character and bail instantly when you hit a wall. Speed comes from simplicity, not cleverness.

How do I avoid off-by-one errors on edge cases?+

Test: empty array, single string, two strings with zero overlap, strings where one is a prefix of another. Your loop should check min length of all strings upfront. If any string is empty, the answer is length 0. Code this defensively.

Can I solve this in one pass?+

Yes. Iterate i from 0 to the length of the shortest string. At each i, check if strings[0][i] == strings[1][i] ==... == strings[n-1][i]. The moment they diverge, return the substring from 0 to i. One pass, no extra structure needed.

What's the space and time complexity they'll ask about?+

Time is O(n * m) where n is the number of strings and m is the length of the shortest string. Space is O(m) for the output. You're not building a trie or DP table, so space is minimal. Communicate this clearly.

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

OA at Uber?
Invisible during screen share
Get it