Reported August 2026
Matroidhash table

Count House Segments After Destruction

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

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

Matroid reported this one in August 2026, and the trap is right in the setup: destroy houses one at a time and recount segments after each. That's a full rescan per query, and it dies on large inputs. The real move is to stop thinking about destruction and think about the segment count changing by a tiny amount each step. It's a hash set plus neighbor check, or a reverse union-find build. If you've got the OA coming up, learn the delta trick below. StealthCoder sits invisibly as a safety net on the live OA if you blank on it.

The problem

You are monitoring building density in a district of houses. The district is represented as a number line, and each house is located at an integer position.
You are given houses, an array containing the initial locations of all houses, and queries, an array containing the locations of houses that will be destroyed, in destruction order.
After each house is destroyed, find the number of house segments that remain. A house segment is a maximal group of one or more houses at consecutive integer positions. In other words, there is no remaining house immediately before or immediately after a segment.
Return an integer array whose i-th value is the number of remaining house segments after the house at queries[i] is destroyed.

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

Examples
Example 1
houses = [1, 2, 3, 6, 7, 9]
queries = [6, 3, 7, 2, 9, 1]
return = [3, 3, 2, 2, 1, 0]
The initial segments are [1, 2, 3], [6, 7], and [9]. Destroying the houses in query order leaves 3, 3, 2, 2, 1, and finally 0 segments.

Constraints
All values in houses are distinct.
Every value in queries appears in houses.
All values in queries are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Brute force recounts segments after every query, which is O(n) per query and O(n^2) total. Skip it. Keep a set of existing houses and a running segment count. When you destroy house x, check whether x-1 and x+1 are still present. Both present: removing x splits one segment into two, so count goes up 1. Neither present: x was a lone segment, so count drops 1. Exactly one present: count stays the same. Compute the initial count by scanning for houses whose left neighbor is missing. Each query is O(1) with a hash set, so the whole thing is O(n). The common pitfall is getting the both-neighbors case backwards, or checking neighbors before removing x from the set. Test on the sample: destroying 3 from [1,2,3] leaves the count at 3. If you freeze on the case logic during the live OA, StealthCoder can hand you the clean version.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count House Segments After Destruction 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Matroid reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count House Segments After Destruction FAQ

What's the trick for Count House Segments After Destruction?+

Track the segment count as a running number and adjust it per query. Check whether the left and right neighbors of the destroyed house still exist. Both exist means +1, neither means -1, one means no change. That gives O(1) per query with a hash set.

How hard is this problem really?+

Easy to medium. The logic is three cases and a set lookup. The difficulty is seeing that you shouldn't recount. Once you see the delta idea, it's about ten lines of code.

Can I solve it with union-find?+

Yes. Process queries in reverse, adding houses back and merging with neighbors, then reverse the recorded counts. It works, but it's more code than the forward delta approach. Use union-find only if the forward case analysis feels shaky.

What edge cases should I test?+

A single house, houses with no adjacent pairs, and one long contiguous block destroyed from the middle. Also test that the final value is 0. Make sure you remove the house from the set before or consistently with your neighbor checks.

How do I prepare for this in 48 hours?+

Write the set-based solution from memory twice. Hand-trace the sample from Matroid's example until the +1, 0, -1 rules feel automatic. Then try a variant where queries aren't distinct, to see what breaks.

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

OA at Matroid?
Invisible during screen share
Get it