Reported September 2024
ZipRecruiterhash table

Longest Contiguous Houses After Each Build

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

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

The ZipRecruiter OA reported in September 2024 hinges on one data structure choice, and it isn't an array. You get house positions in build order, and after each build you return the longest contiguous run of occupied spots. Positions go up to a billion in either direction, so you can't allocate a line. A hash map or union-find solves it in near-linear time for 100000 queries. The hinted pattern says dynamic-programming, but the real trick is merging neighbors. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the solution while you keep typing.

The problem

Initially, no integer position contains a house. The distinct values in queries give house positions in build order.
After every build, return the length of the longest contiguous run of occupied integer positions.

Function
longestHouseSegments(queries: int[]) → int[]

Examples
Example 1
queries = [2,1,3]
return = [1,2,3]
The longest occupied segment grows from {2}, to {1,2}, to {1,2,3}.
Example 2
queries = [1,3,0,4]
return = [1,1,2,2]
The final segments are {0,1} and {3,4}, both of length two.

Constraints
1 <= queries.length <= 100000
-1000000000 <= queries[i] <= 1000000000
All positions are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Keep a hash map from the endpoint of a segment to that segment's length. When you place a house at x, look up the length on the left (x-1) and on the right (x+1), both default to 0. The new length is left + right + 1. Then write that length at the two new endpoints, x-left and x+right. Update a running max and push it to the answer. Interior cells never get read again, so you only maintain endpoints. Union-find with size tracking gives the same result. The common pitfall is allocating an array indexed by position, which dies at a billion. Another is rescanning the segment on every query, which goes quadratic. Also forget duplicates: the input guarantees distinct positions, so you can skip that check. If your mind goes blank during the live OA, StealthCoder is the hedge that surfaces this endpoint-map approach fast.

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 Longest Contiguous Houses After Each Build 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 ZipRecruiter's OA.

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

Longest Contiguous Houses After Each Build FAQ

What's the trick for Longest Contiguous Houses After Each Build?+

Store segment lengths only at segment endpoints in a hash map. When a new house lands at x, read the lengths at x-1 and x+1, add them plus one, and write the total at the two new endpoints. Track the max as you go. Each query is O(1).

Is this really dynamic programming?+

Loosely. Each answer builds on the previous state, which is why it's tagged that way. In practice it's a hash map merge or union-find with sizes. Don't hunt for a DP table. The coordinate range of a billion rules out any array-based table anyway.

Why can't I just use a boolean array?+

Positions range from -1000000000 to 1000000000. An array that size blows memory. You'd need coordinate compression, which works offline, or just use a hash map keyed by position. The map approach is simpler and handles negatives without any offset.

Can union-find solve it instead of the endpoint map?+

Yes. Map each position to an index, union with x-1 and x+1 when they exist, and track component sizes. Update the max after each union. It's slightly more code than the endpoint trick but equally fast. Pick whichever you can write without bugs under pressure.

How do I prepare for this in 48 hours?+

Write the endpoint-map solution twice from scratch, then test both examples: [2,1,3] gives [1,2,3] and [1,3,0,4] gives [1,1,2,2]. Add a negative position and a single-element case. Also practice similar merge-interval problems like longest consecutive sequence. That covers the pattern.

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

OA at ZipRecruiter?
Invisible during screen share
Get it