Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
n = 10⁵ points are given, followed by q = 10⁵ OFFLINE queries 'how many points lie in the axis-aligned rectangle [x1, x2] × [y1, y2]?'. Which approach answers all queries within about O((n + q) log n)?
- A. A 2-D prefix-sum array over the coordinate range
- B. A k-d tree, which guarantees O(log n) per query
- C. An offline sweep over x with a Fenwick tree on compressed y, four prefix counts per query
- D. Sort points by x and binary search each query's x-range only
Correct answer: C. An offline sweep over x with a Fenwick tree on compressed y, four prefix counts per query
A sweep over x with a BIT on y answers 'points with x ≤ X and y ≤ Y' in O(log n), and inclusion–exclusion combines four such counts. A 2-D prefix array needs memory proportional to the coordinate range (often 10⁹ × 10⁹ cells); a k-d tree gives O(√n) per rectangle query; filtering only by x leaves an O(n) scan.
More Coding Interview Problems questions
- Edge case: integer division and modulo with negative operands in Python. python print(-7 // 2, -7 % 2, int(-7 / 2)) What is printed?
- Problem: Replace at most k characters so that the longest possible substring consists of one repeated letter; return its length. python…
- Problem: Smallest length of a contiguous subarray of POSITIVE integers with sum ≥ target (0 if none). python def minlen(a, t): l, s, best…
- Problem: Return all start indices of anagrams of p inside s using a fixed-size window of letter counts. python from collections import…
- Problem: Intersect two lists of disjoint, sorted closed intervals. python def inter(A, B): i = j = 0; out = [] while i < len(A) and j <…
- Problem: Cars drive toward a target on a one-lane road. A car that catches a slower car ahead joins it as a fleet (moving at the slower…