Rotate matrix and find max lamp coverage
Company: Capital One
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates array and matrix manipulation, in-place algorithm design, interval processing (sweep-line) and interval merging skills, testing competencies in algorithm design, complexity analysis, and robust edge-case reasoning within the Coding & Algorithms domain.
Rotate Square Matrix 90 Degrees Clockwise
Constraints
- 1 <= n <= 1000
- matrix is square (n x n)
- -10^9 <= matrix[i][j] <= 10^9
Examples
Input: ([[1,2,3],[4,5,6],[7,8,9]],)
Expected Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
Explanation: Each row becomes a column: row [1,2,3] becomes the last column (top-to-bottom 1,2,3 -> read as col with 1 at bottom).
Input: ([[1,2],[3,4]],)
Expected Output: [[3, 1], [4, 2]]
Explanation: 2x2 clockwise rotation; top-left 1 moves to top-right.
Input: ([[42]],)
Expected Output: [[42]]
Explanation: 1x1 matrix rotates to itself.
Input: ([[-1,-2],[-3,-4]],)
Expected Output: [[-3, -1], [-4, -2]]
Explanation: Negative values rotate positionally just like positive ones.
Input: ([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],)
Expected Output: [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]
Explanation: 4x4 (even n): first column of output is the bottom row of input read upward.
Input: ([[0,5],[-7,3]],)
Expected Output: [[-7, 0], [3, 5]]
Explanation: Mixed signs and zero; purely positional.
Hints
- Element (i, j) of the input lands at (j, n-1-i) of the output. Allocate a fresh n x n matrix and scatter each element into its destination.
- Equivalently, the i-th row (read left to right) becomes the (n-1-i)-th column (read top to bottom).
- For the in-place follow-up: transpose the matrix (swap matrix[i][j] with matrix[j][i] for i < j), then reverse each row.
- Rotation only moves elements around, so negative values and the center cell of an odd-sized matrix need no special handling.
Maximum Lamp Coverage on a Number Line
Constraints
- 1 <= N <= 2*10^5
- -10^9 <= x_i <= 10^9
- 1 <= r_i <= 10^9
- Intervals are CLOSED: [x_i - r_i, x_i + r_i]
Examples
Input: ([[0,2],[3,2],[6,2]],)
Expected Output: (2, [[1, 2], [4, 5]])
Explanation: Intervals [-2,2], [1,5], [4,8]. First two overlap on [1,2] (count 2); last two overlap on [4,5] (count 2). Max is 2 on those two closed segments.
Input: ([[5,3]],)
Expected Output: (1, [[2, 8]])
Explanation: Single lamp covers [2,8] with count 1 everywhere inside it.
Input: ([[0,1],[0,1],[0,1]],)
Expected Output: (3, [[-1, 1]])
Explanation: Three identical intervals [-1,1] stack to count 3 across the whole shared segment.
Input: ([[0,1],[10,1]],)
Expected Output: (1, [[-1, 1], [9, 11]])
Explanation: Two disjoint lamps; max count is 1 achieved separately on each interval.
Input: ([[0,5],[1,1],[8,1]],)
Expected Output: (2, [[0, 2]])
Explanation: Intervals [-5,5], [0,2], [7,9]. The big lamp and the small lamp at 1 overlap on [0,2] (count 2); the lamp at 8 only overlaps nothing else, so the unique max segment is [0,2].
Input: ([[-3,2],[2,3]],)
Expected Output: (2, [[-1, -1]])
Explanation: Intervals [-5,-1] and [-1,5] touch only at the single closed point -1, where both count, giving a degenerate max segment [-1,-1].
Hints
- Convert each lamp into the closed interval [x - r, x + r]. The coverage at a point p is (number of intervals whose left endpoint <= p) minus (number whose right endpoint < p) — two binary searches over sorted endpoint arrays.
- Coverage is a step function that only changes at the 2N endpoint coordinates. The maximum is achieved on a union of closed regions whose boundaries are these critical coordinates.
- Sweep the sorted distinct critical coordinates. Evaluate coverage both AT each critical point and ON the open gap to its right; record every region whose coverage equals the global maximum, then merge adjacent max regions into maximal closed segments.
- Because intervals are closed, a coordinate where one interval starts and another ends still counts both — process start (+1) events before end (-1) events at equal coordinates.
- Follow-up: for OPEN intervals the endpoints stop counting, so max segments become open; for integer-only coordinates you only need to compare coverage at integer points.