Coding questions · Zillow
Find max min-plus-max over all subarrays
Find max min-plus-max over all subarrays evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
You are given an array arr of n integers.
A subarray is a contiguous non-empty segment of the array. For any subarray arr[l..r] (0-indexed), define:
Your task is to compute the maximum possible value of X(l, r) over all subarrays of arr.
Input
- An integer
n(the length of the array). - An array
arrofnintegers (can be positive, zero, or negative).
Output
- A single integer: the maximum value of
min(subarray) + max(subarray)over all possible non-empty contiguous subarrays.
Requirements
- Describe an efficient algorithm and its time and space complexity.
- Aim for better than time for large
n(e.g.,nup to around2 * 10^5).
What a Strong Answer Covers
- A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
- A correctness argument that explains why the algorithm covers all required cases.
- Time and space complexity, plus at least one alternative approach when relevant.
- Focused tests for normal cases, edge cases, and failure modes.
Follow-up Questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
Maximize Min Plus Max Over All Subarrays
Examples
Example 1
Input: ([1,3,2],)
Output: 6
Explanation: Single-element subarray at max gives 6.
Example 2
Input: ([-5,-2,-3],)
Output: -4
Explanation: Least negative value.
Hints
- Model object-style prompts as arrays or operation streams when needed.
- Handle empty and boundary cases before the main logic.
Solution
Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
Problem Restatement
You are given an array arr of n integers. A subarray is a contiguous non-empty segment of the array. For any subarray arr[l..r] (0-indexed), define:
Your task is to compute the maximum possible value of X(l, r) over all subarrays of arr. ### Input - An integer n (the length of the array). - An array arr of n integers (can be positive, zero, or negative). ### Output - A single integer: the maximum value of min(subarray) + max(subarray) over all possible non-empty contiguous subarrays. ### Requirements - Describe an efficient algorithm and its time and space complexity. - Aim for better than time for large n (...
Recommended Approach
Record the last occurrence of each digit 0-9. Scan digits from left to right; for the current digit d, look for a larger digit from 9 down to d+1 whose last occurrence is to the right. The first such swap yields the lexicographically largest possible number, so perform it and stop.
Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
Complexity
Time: O(L * 10) = O(L). Space: O(1).
Edge Cases and Tests
Already maximal number, repeated digits, leading digit swaps, single digit, all equal digits, and string input that may exceed integer range.