Count House Segments After Destruction
Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Capital One reported this one in May 2026, and the trap is baked into the title. Count House Segments After Destruction looks like a simulation, so most people rescan the array after every query and then wonder why it crawls. You've got an OA coming up, so know this now: it's a set lookup plus a neighbor check. Each destroyed house either splits a segment, shrinks one, or wipes one out, and you can tell which in constant time. If you blank on the case logic during the live assessment, StealthCoder is the safety net running invisibly on your screen.
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 destroyed in order. After each destroyed house, return the number of house segments remaining. A house segment is one or more adjacent houses whose positions are consecutive integers and which do not have neighboring houses immediately outside the segment. Function countHouseSegmentsAfterDestruction(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] Initially the house segments are [1, 2, 3], [6, 7], and [9]. Removing the houses in query order leaves 3, 3, 2, 2, 1, and then 0 segments. Example 2 houses = [2, 4, 5, 6, 7] queries = [5, 6, 2] return = [3, 3, 2] After removing 5, the segments are [2], [4], and [6, 7]. After removing 6, the segments are [2], [4], and [7]. After removing 2, two segments remain. Constraints All values in houses are distinct. Every value in queries appears in houses, and all values in queries are distinct.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a hash set of the houses still standing and a running segment count. Start by counting segments: a house starts one if house-1 isn't in the set. For each query x, check whether x-1 and x+1 are present. Both present: removing x splits one segment into two, so count goes up by 1. Neither present: x was a lone segment, so count goes down by 1. Exactly one present: the segment just shrinks, count unchanged. Append the count after each query. That's O(n + q) total. The first-attempt mistake is recounting from scratch per query, which is O(n*q), or forgetting to delete x from the set before the next query. Another slip is mixing up the split case with the shrink case. Check example 2: removing 5 from [4,5,6,7] splits it, giving 3. StealthCoder is your hedge if the case table slips under pressure in the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Capital One's OA.
Capital One 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 in Count House Segments After Destruction?+
Don't recount. Track the segment count incrementally and look only at the destroyed house's two neighbors. Both neighbors alive means a split, plus one. Neither alive means a lone house vanished, minus one. One neighbor alive means no change. A hash set makes each check constant time.
How hard is this Capital One OA question really?+
Easy to medium. The code is short, maybe 15 lines. The difficulty is seeing that removal is a local event instead of a global recount. Once you write the three neighbor cases, it's done. Most failures come from brute force timing out on large inputs.
Do I need union-find or reverse processing?+
No. Union-find would fit if you added houses in reverse, but the direct approach works fine. Deleting from a set and checking x-1 and x+1 handles every case in O(1). Reverse union-find is overkill here and adds bug surface.
What edge cases should I test?+
Test a single house, a query that removes a lone house, and a removal from the middle of a long run. Also test removing an endpoint of a run. Run example 1 by hand: the answer should be 3, 3, 2, 2, 1, 0. Make sure you remove x from the set after computing.
How do I prepare for this in 48 hours?+
Write this solution from memory twice. Then do two or three other set-and-neighbor problems, like consecutive sequence or interval merging. The point is recognizing local updates over recomputation. Don't grind widely. Learn to spot when a global count changes only by what's adjacent.