Maximal Square and Longest Increasing Subsequence
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
Implement `analyze(matrix, nums)` and return `[max_square_area, lis_length]`.
### Part A: Maximal Square
`matrix` is a rectangular grid containing only `0` and `1`. Return the area of the largest axis-aligned square containing only `1` values. Return `0` for an empty matrix.
### Part B: Longest Increasing Subsequence
`nums` is an integer array. Return the length of its longest strictly increasing subsequence. A subsequence preserves order but need not be contiguous. Return `0` for an empty array.
## Constraints
- At most 500 rows and 500 columns in `matrix`
- `0 <= len(nums) <= 200,000`
- `-10^9 <= nums[i] <= 10^9`
## Example
For
```text
matrix = [[1,0,1,0,0],
[1,0,1,1,1],
[1,1,1,1,1],
[1,0,0,1,0]]
nums = [10,9,2,5,3,7,101,18]
```
return `[4, 4]`.
## Clarifications
The square result is area, not side length. Equal values cannot both extend a strictly increasing subsequence.
## Hint
For the square, relate a cell to three neighboring subproblems. For the subsequence, maintain the smallest possible tail value for each achievable length.
## Interview Follow-ups
- Present brute-force and optimal approaches for both parts.
- Reduce maximal-square auxiliary space to one row.
- Reconstruct one longest increasing subsequence.
Quick Answer: Implement two independent analyses: the largest all-ones square in a binary matrix and the length of a strictly increasing subsequence. Account for empty inputs, area-versus-side-length semantics, duplicate values, very large sequences, complexity comparisons, reduced auxiliary space, and witness reconstruction.