Implement 2D transforms and find max-lit point
Capital OneSoftware Engineer · Online AssessmentLast updated
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.
Part A — 2D array transforms: Given an m×n integer matrix, implement the following in-place operations with clear function boundaries and complexity: (
- swapRows(A, i, j) that swaps two row indices i and j; (
- swapCols(A, i, j) that swaps two column indices i and j; (
- reverseRowOrder(A) that reverses the order of rows (top↔bottom) and reverseColOrder(A) that reverses the order of columns (left↔right); (
- rotate90(A, direction) that rotates the matrix by 90 degrees in the specified direction (clockwise or counterclockwise), supporting both square and rectangular matrices (for rectangular, return a new matrix or explain constraints). Specify time and space complexities and any edge-case handling (e.g., empty matrix, single row/column). Part B — most-illuminated point: You are given a list of street lights as pairs (x, r) where x is the light’s coordinate on a 1D line and r is its illumination radius. Each light illuminates the interval [x − r, x + r]. Find the point on the line covered by the maximum number of lights; if multiple points achieve the same maximum coverage, return the smallest such coordinate. Describe and implement an algorithm with optimal time complexity, and analyze time and space.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.