Count queen attacks on points with blockers
Company: Voleon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates spatial reasoning on an integer 2D grid, geometric line relationships for queens' attack paths, and the use of efficient data structures to count interactions and handle occlusion, in the Coding & Algorithms domain.
Part 1: Count Queen Attacks Without Blockers
Constraints
- 0 <= len(queens) <= 2 * 10^5
- 0 <= len(points) <= 2 * 10^5
- Coordinates fit in 32-bit signed integers.
- Query points may repeat.
- If a queen is exactly on a query point, that queen does not count as attacking that point.
Examples
Input: ([[0, 0], [1, 2], [3, 0], [2, 2]], [[0, 2], [2, 0], [1, 1], [5, 5]])
Expected Output: [3, 3, 3, 2]
Explanation: For example, [0, 2] is attacked by two queens on row y=2 and one queen on column x=0.
Input: ([], [[0, 0], [1, 1]])
Expected Output: [0, 0]
Explanation: With no queens, no point is attacked.
Hints
- A queen can attack a point based only on four line identifiers: row, column, x - y diagonal, and x + y diagonal.
- Be careful not to count a queen on the query point itself four times.
Part 2: Count Queen Attacks With Blocking Rocks
Constraints
- 0 <= len(queens) <= 2 * 10^5
- 0 <= len(points) <= 2 * 10^5
- 0 <= len(rocks) <= 2 * 10^5
- Coordinates fit in 32-bit signed integers.
- Queens and rocks do not occupy the same coordinate.
- Query points may repeat and may coincide with a queen or a rock.
Examples
Input: ([[0, 0], [0, 3], [3, 0], [2, 2], [-2, 2]], [[0, 2], [0, -1], [1, 1], [3, 3]], [[0, 1], [1, 1], [2, 0]])
Expected Output: [3, 1, 2, 3]
Explanation: For [0,2], the rock at [0,1] blocks the queen at [0,0], but not the queen at [0,3]. The query [1,1] is itself a rock, but endpoint rocks are not strictly between.
Input: ([[0, 0], [1, 1], [2, 0]], [[1, 0], [1, 1], [3, 3]], [])
Expected Output: [3, 2, 2]
Explanation: With no rocks, this reduces to the no-blockers version.
Hints
- Handle each row, column, and diagonal as a separate sorted 1D line.
- For a query point on a line, only queens between the nearest rock before the point and the nearest rock after the point can attack along that line.