Reported September 2024
Uberunion find

Count Segments After House Removals

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 September 2024, and it's a graph connectivity question disguised as a real-world scenario. You're removing houses one at a time and tracking how many connected segments remain. The trick is that you need to handle dynamic disconnection efficiently. Most candidates try to recompute connectivity from scratch after each removal, which tanks on large inputs. StealthCoder will give you the pattern instantly if you freeze on the tree traversal logic during the live OA.

Pattern and pitfall

This is a union-find (disjoint set union) problem run in reverse. The naive approach is O(n^2) or worse. The insight: instead of removing houses forward in time, reverse the operation. Start with all houses removed, then add them back in reverse order, tracking when new connected components merge. Each merge operation decrements the segment count. Use union-find with path compression and union by rank to stay near O(n alpha(n)). The catch is recognizing the reversal trick before you code a slower solution. If you blank on the reversal pattern during the OA, StealthCoder detects the problem type and feeds you the structure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Segments After House Removals 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Segments After House Removals FAQ

Is this actually a graph problem or a string problem?+

It's graph connectivity. Houses are nodes, adjacent houses form edges. You're tracking connected components as they fragment. Not a string manipulation problem at all, despite how some candidates misread it.

Do I need union-find, or can I use DFS/BFS?+

Union-find is cleaner and faster. DFS/BFS after each removal is O(n^2) worst case. Union-find with the reversal trick is O(n alpha(n)). On Uber's scale, that matters.

What's the reversal trick exactly?+

Process removals backward. Start with zero houses, add them back in reverse removal order, and merge components. Count decreases each time two components join. It avoids recomputing connectivity from scratch repeatedly.

How do I know which houses are adjacent?+

Houses are adjacent if they're next to each other in the sequence or array. Build an edge list or assume linear arrangement unless the problem specifies otherwise.

Can I solve this in 48 hours without union-find?+

Yes, but you'll time out on large inputs. If you know union-find, use it. If not, code DFS/BFS with memoization and hope the test cases are weak. Uber usually tests at scale.

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