Quick Overview

Track the number of consecutive house segments after each requested demolition on an integer line. Each removal can eliminate an isolated segment, trim an endpoint, or split a run, so the solution must update the count efficiently across many distinct positions.

Count House Segments After Sequential Demolitions

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

## Problem Distinct houses occupy integer positions on a number line. Houses at consecutive positions belong to the same segment. Remove houses in the given query order and return the number of remaining segments after each removal. ### Function Contract Implement `segment_counts_after_removals(houses, queries) -> list[int]`. ### Constraints - `1 <= len(houses) <= 200000` and all positions are distinct. - Every query position occurs in `houses`, and query positions are distinct. - `1 <= len(queries) <= len(houses)`. - House positions are integers in `[-10^9, 10^9]`. ### Examples - `houses = [1,2,3,6,7,9]`, `queries = [6,3,7,2,9,1]` returns `[3,3,2,2,1,0]`. - Removing the only house returns `[0]`. ```hint Inspect immediate neighbors Removing one active house changes only the adjacency relationships with positions one less and one greater. ``` ```hint Or process in reverse Adding queried houses back in reverse turns deletion into a union operation; remember that houses never queried remain active initially. ``` ### Edge Cases - Removing an isolated house reduces the segment count by one. - Removing the middle of a segment splits it into two and increases the count by one. - Removing an endpoint leaves the number of segments unchanged unless it was a one-house segment.

Overview: Track the number of consecutive house segments after each requested demolition on an integer line. Each removal can eliminate an isolated segment, trim an endpoint, or split a run, so the solution must update the count efficiently across many distinct positions.

Read the full ByteDance Software Engineer interview experience this question came from

Distinct houses occupy integer positions on a number line. Active houses at consecutive integer positions belong to the same segment. Remove the queried houses in the given order and return the number of remaining segments after each removal. Every query names a distinct position present in the original houses array; houses not queried remain active.

Constraints

  • 1 <= len(houses) <= 200000, and all house positions are distinct.
  • 1 <= len(queries) <= len(houses).
  • Every query position occurs in houses, and query positions are distinct.
  • House positions are integers in [-1000000000, 1000000000].

Examples

Input: ([1, 2, 3, 6, 7, 9], [6, 3, 7, 2, 9, 1])

Expected Output: [3, 3, 2, 2, 1, 0]

Explanation: This is the source example and covers endpoint, isolated, and final removals.

Input: ([5], [5])

Expected Output: [0]

Explanation: Removing the only house removes the only segment.

Hints

  1. Removing one active house changes only its adjacency to positions one less and one greater.
  2. Classify the removed house as a middle, endpoint, or isolated house from those two membership checks.

Loading coding console...