Implement 2D transforms and find max-lit point
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates array-manipulation and interval-analysis skills, specifically in-place 2D matrix transformations (row/column swaps, reversals, rotations) and determining the point with maximum overlap among one-dimensional illumination intervals.
Rotate a Matrix by 90 Degrees (Clockwise / Counterclockwise)
Constraints
- 0 <= m, n
- direction is exactly 'clockwise' or 'counterclockwise'
- matrix may be empty, a single row, a single column, or a single cell
- rectangular matrices are supported: result has shape n x m
Examples
Input: ([[1, 2, 3], [4, 5, 6]], 'clockwise')
Expected Output: [[4, 1], [5, 2], [6, 3]]
Explanation: 2x3 rotated clockwise becomes 3x2; first result row [4,1] is the first input column read bottom-to-top.
Input: ([[1, 2, 3], [4, 5, 6]], 'counterclockwise')
Expected Output: [[3, 6], [2, 5], [1, 4]]
Explanation: Counterclockwise: first result row [3,6] is the last input column read top-to-bottom.
Input: ([[1, 2], [3, 4]], 'clockwise')
Expected Output: [[3, 1], [4, 2]]
Explanation: Square 2x2 clockwise rotation.
Input: ([[1, 2], [3, 4]], 'counterclockwise')
Expected Output: [[2, 4], [1, 3]]
Explanation: Square 2x2 counterclockwise rotation.
Input: ([[7]], 'clockwise')
Expected Output: [[7]]
Explanation: Single cell is unchanged by rotation.
Input: ([], 'clockwise')
Expected Output: []
Explanation: Empty matrix edge case returns empty.
Input: ([[1, 2, 3, 4]], 'clockwise')
Expected Output: [[1], [2], [3], [4]]
Explanation: Single row rotated clockwise becomes a single column in the same order.
Input: ([[1], [2], [3]], 'counterclockwise')
Expected Output: [[1, 2, 3]]
Explanation: Single column rotated counterclockwise becomes a single row in the same order.
Hints
- A clockwise 90-degree rotation maps input cell (r, c) to output cell (c, m-1-r). Invert this to write the result directly: result[i][j] = matrix[m-1-j][i].
- For rectangular matrices the result shape flips to n x m, so you cannot rotate fully in place — allocate the n x m output.
- Counterclockwise is the mirror: result[i][j] = matrix[j][n-1-i]. Check single-row and single-column inputs first.
Most-Illuminated Point on a Line
Constraints
- 0 <= number of lights
- each light is a pair (x, r) with r >= 0
- the interval is closed: [x - r, x + r]
- ties in coverage are broken by the smallest coordinate
- return None when there are no lights
Examples
Input: ([[2, 4], [4, 1]],)
Expected Output: 3
Explanation: Intervals are [-2,6] and [3,5]. They overlap on [3,5] with coverage 2. The smallest point with coverage 2 is 3.
Input: ([[0, 1], [5, 1]],)
Expected Output: -1
Explanation: Intervals [-1,1] and [4,6] do not overlap; max coverage is 1; the smallest covered coordinate is -1.
Input: ([[0, 2], [1, 2], [10, 1]],)
Expected Output: -1
Explanation: Intervals [-2,2] and [-1,3] overlap; at -1 both cover it (coverage 2), which is the smallest such point; [9,11] is isolated.
Input: ([[5, 0]],)
Expected Output: 5
Explanation: A single zero-radius light illuminates only the point 5.
Input: ([],)
Expected Output: None
Explanation: No lights -> None.
Input: ([[0, 5], [0, 5], [0, 5]],)
Expected Output: -5
Explanation: Three identical intervals [-5,5] give coverage 3 everywhere; the smallest covered coordinate is -5.
Input: ([[-3, 1], [-3, 1]],)
Expected Output: -4
Explanation: Two identical intervals [-4,-2] with coverage 2; smallest point is -4.
Input: ([[1, 1], [3, 1], [5, 1]],)
Expected Output: 2
Explanation: Intervals [0,2],[2,4],[4,6]. At 2, [0,2] (closed) and [2,4] both cover -> coverage 2; 2 is the smallest such point (the chain also overlaps at 4).
Hints
- The answer can always be placed at some interval's left endpoint x - r, so you only need to evaluate coverage at those coordinates.
- Use a sweep line: emit +1 at x-r and -1 at x+r. Process start events before end events at the same coordinate so a point touched by an interval's edge still counts (closed intervals).
- Scan coordinates in increasing order and only update the answer on a STRICT increase in coverage — this automatically returns the smallest coordinate when several points tie for the maximum.