Reported October 2022
ZipRecruiterhash table

House Segments After Removals

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 data structure is the whole question here. ZipRecruiter reported this one in October 2022: houses sit at distinct integer positions, you remove them one at a time, and after each removal you report how many contiguous runs remain. With up to 100000 houses, rescanning after every query is dead on arrival. The clean move is to run the removals backwards and treat it as adding houses with union-find. If the idea doesn't come when the timer's running, StealthCoder is the invisible safety net on the live OA. It reads the problem and hands you the approach.

The problem

houses lists distinct occupied integer positions. Every value in queries names a currently existing house to remove.
After each removal, return the number of maximal contiguous runs of remaining house positions.

Function
houseSegmentsAfterRemovals(houses: int[], queries: int[]) → int[]

Examples
Example 1
houses = [1,2,4,5]
queries = [2,4,1,5]
return = [2,2,1,0]
Removing 2 and then 4 leaves two separated singleton segments; only one segment remains after removing 1.
Example 2
houses = [3]
queries = [3]
return = [0]
Removing the only house leaves no segments.

Constraints
1 <= houses.length == queries.length <= 100000
Each house is removed exactly once.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is reversal. Removing houses and counting segments is awkward. Adding houses and counting segments is easy. Start with an empty set, then process queries from last to first. Each added house starts as a new segment, so count goes up by one. Then check whether position-1 and position+1 are present. Each present neighbor you merge with drops the count by one. Union-find works, but you don't even need it. A hash set of present positions is enough, since the count only depends on the neighbors. Record the count before each add, shifted correctly: answer[i] is the count after removal i, which equals the count in the reversed build just before re-adding queries[i]. The common pitfall is an off-by-one in that shift. Another is simulating forward with a sorted list and deleting, which turns O(n) per removal into a timeout. Check example 1 by hand: the final answer is 0 and the first is 2.

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 House Segments After 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

House Segments After Removals FAQ

What's the trick for House Segments After Removals?+

Reverse the queries. Removing houses is hard to track, but adding them is simple. Each new house adds one segment, then subtract one for every adjacent house already present on the left or right. Store counts and flip the order at the end.

Do I need union-find for this ZipRecruiter problem?+

No. A hash set of present positions is enough. When you add a house at p, you only care whether p-1 and p+1 are already in the set. Union-find works but adds code and bug surface without changing the answer.

What complexity should I aim for with 100000 houses?+

O(n) with a hash set, or O(n log n) if you sort or use union-find. Anything that rescans the houses after each removal is O(n squared) and will time out at the upper constraint of 100000.

How do I avoid off-by-one errors in the output?+

Answer[i] is the segment count after removing queries[i]. In the reversed build, that's the count before you re-add queries[i]. So for i from last to first, record the current count, then add the house and update. Test with example 1.

How do I prepare for this in 48 hours?+

Practice the reverse-and-add idea on two or three problems where deletions are painful, like dynamic connectivity offline. Then code this one from scratch with a set. Test the single-house case and a fully contiguous input, since those catch most bugs.

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