Reported August 2026
TikTokhash table

Count House Segments After Destruction

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

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

The TikTok OA reported in August 2026 looks like a simple counting task until you hit the edge case: removing a house can split a segment, shrink it, or delete it entirely. If you rescan the array after every query, you're stuck at O(n*q). This is a hash-set neighbor check problem, and the trick takes about ten lines once you see it. You have a day or two, so read the neighbor logic below and you'll be fine. If you freeze mid-assessment, StealthCoder runs invisibly as a safety net and surfaces the approach while you type.

The problem

You are monitoring building density in a district represented as a number line. Every 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 destruction, count the house segments that remain.
A house segment is one or more houses at consecutive integer positions, with no house immediately before or immediately after the segment.
Return an array whose i-th value is the number of remaining house segments after destroying the house at queries[i].

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 [4] and [6,7], so three segments remain together with [2]. Removing 6 keeps three segments, and removing 2 leaves two.
Example 3
houses = [-1,0,1,5]
queries = [0,5]
return = [3,2]
Destroying the middle house at 0 splits [-1,0,1] into two single-house segments while [5] remains, giving 3. Removing 5 then leaves the two single-house 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

Keep a 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, check whether x-1 and x+1 are present. Both present: the segment splits, so count goes up by 1. Neither present: it was a lone house, so count goes down by 1. Exactly one present: the segment just shrinks, so count stays the same. Append the count after each removal, then delete x from the set. The common pitfall is deleting x before checking neighbors, or treating the 'one neighbor' case as a change. Negative positions, like Example 3, work fine with a set but break any array indexed by position. That's why you shouldn't allocate an array sized to max value. Total time is O(n+q). If your mind goes blank on the split case, StealthCoder can give you the three-way branch as a hedge during the live OA.

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 TikTok's OA.

TikTok 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

How hard is the TikTok Count House Segments problem really?+

Easy to medium. The idea is a three-way neighbor check, and the code is short. Most failures come from ordering mistakes or from brute force that rescans everything per query. If you write the O(n+q) hash-set version, you're done.

What's the trick to solve it fast?+

Track the segment count incrementally. Before removing x, check whether x-1 and x+1 are in the set. Both there means +1 segment. Neither means -1. One there means no change. Then remove x and record the count.

Why does the naive approach fail?+

Recomputing segments after each destruction costs O(n) per query, so O(n*q) overall. With large inputs that times out. The incremental neighbor check makes each query O(1) on average with a hash set.

Can positions be negative, and does that matter?+

Yes, Example 3 includes -1. Use a hash set or map instead of an array indexed by position. Offsetting by the minimum works too, but a set is simpler and avoids any range issues.

How do I prepare for this in 48 hours?+

Hand-trace Example 2 until the three cases feel automatic. Then code it once from scratch with a set, and test a lone house, an end-of-segment removal, and a middle removal. Twenty minutes of that covers this pattern.

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

OA at TikTok?
Invisible during screen share
Get it