Count House Segments After Destruction
Reported by candidates from Roblox's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The first attempt at this Roblox OA from August 2026 usually dies the same way: recomputing the segment count from scratch after every destruction. That's O(n^2) and it times out. The problem, Count House Segments After Destruction, hands you a set of houses on a number line and a list of queries that knock them out one by one. You report how many maximal consecutive groups remain after each hit. The trick is local. Each removal only affects its two neighbors. If you blank on the neighbor logic, StealthCoder is the safety net running invisibly during the live assessment.
The problem
You are monitoring the building density in a district of houses. The district is represented as a number line, where each house is located at an integer position. Some of the houses are gradually destroyed over time. You are given houses, an array of integers representing the initial locations of all houses in the district. You are also given queries, an array of integers representing the locations of houses that will be destroyed, in the order in which they are destroyed. After each house is destroyed, find the number of house segments remaining in the district. A house segment is a maximal group of one or more houses at consecutive integer positions. Return an array of integers representing the number of house segments after each respective house from queries 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 house segments are [1, 2, 3], [6, 7], and [9]. Destroying houses in the order [6, 3, 7, 2, 9, 1] leaves [3, 3, 2, 2, 1, 0] segments after the respective operations. Constraints All house locations in houses are distinct. Every house location in queries is present in houses. All house locations in queries are distinct.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Don't rebuild segments. Put all houses in a hash set and count the initial segments: a house starts a segment if house-1 isn't in the set. Then process each query x. Check whether x-1 and x+1 exist. Both present: removing x splits one segment into two, so count +1. Neither present: x was a lone segment, so count -1. Exactly one present: the segment just shrinks, count unchanged. Remove x from the set and push the count. That's O(n) total. The common pitfall is checking neighbors after deleting x, or forgetting the isolated house case. Another trap is handling the example's order wrong: the answer is after each destruction, not before. Walk Example 1 by hand once. Destroying 6 leaves 7 alone, count stays 3. Destroying 3 shrinks [1,2,3], still 3. If the neighbor cases slip your mind mid-assessment, StealthCoder can hand you the clean version while you keep typing normally.
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 Roblox's OA.
Roblox 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?+
Only the two neighbors of the destroyed house matter. Check x-1 and x+1 in a set. Both present means a split, so +1. Neither present means a lone house vanished, so -1. One present means no change. Count the initial segments once, then update in O(1) per query.
How hard is this Roblox OA problem really?+
Easy to medium. The brute force is obvious and too slow, but the fix is a short case analysis on neighbors. If you've seen segment counting or island-style problems, you can finish it in about fifteen minutes. The risk is sloppy edge cases, not the algorithm.
Should I use a set or union-find?+
A hash set with neighbor checks is simpler and enough. Union-find handles merging, but here houses get removed, which splits groups. You could reverse the queries and merge, but that's extra work. Stick with the set and the three-case update.
What edge cases break most first attempts?+
Deleting x from the set before checking its neighbors is the big one. Others are miscounting initial segments, forgetting that an isolated house reduces the count, and returning counts before the destruction instead of after. Test with a single house and with queries that empty the district.
How do I prepare for this in 48 hours?+
Write this one solution from memory twice. Then practice a few hash-set neighbor problems, like longest consecutive sequence. Focus on reasoning about local changes instead of recomputing the whole state. Know your O(n) time and O(n) space answer in case someone asks.