Count House Segments After Sequential Destruction
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The first example in this ByteDance problem starts with houses at 1, 2, 3, 6, 7 and 9, which makes three segments before a single query runs. That's the whole game. ByteDance reported this OA in August 2026, and it looks like a reverse-process problem wearing a simulation costume. Destroying houses one at a time means splits, and splits are annoying to track. Run the queries backward and every step becomes a merge. That's union-find territory, or a neighbor-check with a hash set. If you blank on the reversal idea during the live assessment, StealthCoder can sit invisibly on your screen as a safety net.
The problem
Given an array houses of distinct integer house locations and an array queries of distinct locations that all appear in houses, destroy the queried houses in order. A house segment is a maximal group of remaining houses at consecutive integer locations. After each destruction, record the number of remaining segments. Return one segment count for every query, in query order. 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 segments are [1, 2, 3], [6, 7], and [9]. Removing the houses in query order leaves 3, 3, 2, 2, 1, and finally 0 segments. Example 2 houses = [2, 4, 5, 6, 7] queries = [5, 6, 2] return = [3, 3, 2] Removing 5 splits [4, 5, 6, 7] into two segments. Removing 6 preserves the segment count, and removing 2 leaves two segments. Constraints 1 <= houses.length <= 100000 1 <= queries.length <= houses.length -10^9 <= houses[i] <= 10^9 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
The trick is time reversal. Forward, removing a house can split a segment (+1), shrink it (0), or delete it (-1). Backward, adding a house is cleaner: it creates a new segment, then merges with the left neighbor if present, and the right neighbor if present. Segment count goes +1, then -1 for each merge. Start with the houses that are never queried. Here queries may be a subset of houses, so build those first and count their segments. Then process queries in reverse, recording the count before each add, and shift the output accordingly. The common pitfall is off-by-one in the answer array: the answer for query i is the count after destroying i, which equals the count before re-adding it in reverse. Use union-find over indices of sorted houses, or a hash map of segment endpoints. Sorting costs O(n log n), fine for 100000. If the reversal doesn't click under pressure, StealthCoder is your hedge for the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count House Segments After Sequential 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ByteDance's OA.
ByteDance 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.
Count House Segments After Sequential Destruction FAQ
What's the trick in the ByteDance house segments problem?+
Reverse the queries. Destroying houses causes splits that are hard to track, but adding them back only creates or merges segments. Each add is +1 segment, then -1 per adjacent existing house that joins it. That gives O(n log n) with sorting plus union-find, or near linear with hashing.
Do I need union-find or is a hash set enough?+
Either works. With a hash set of present locations, check whether x-1 and x+1 exist when re-adding x. The segment count changes by 1 minus the number of existing neighbors. You don't even need real merging, since you only count segments, not their sizes.
How do I handle houses that are never queried?+
Queries can be a subset of houses, so some houses survive to the end. Insert all of those first, count their segments, then process queries in reverse. Forgetting this base state is the most common wrong answer on the examples.
How hard is this one really?+
Medium. The code is short once you see the reversal. The difficulty is spotting it. A brute force that recounts segments after every query is O(n squared), which fails at 100000 houses.
How do I prepare in 48 hours for this ByteDance OA?+
Practice reverse-process problems and neighbor-merge counting. Write the reversed loop once from scratch, then test it on both examples, especially the second one where removing 6 leaves the count unchanged. Check your answer array ordering carefully.