Determine balanced k values in a permutation
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates algorithmic reasoning about permutations and contiguous subarray properties, testing skills in array manipulation, positional reasoning, combinatorial insight, and complexity-aware algorithm design within the Coding & Algorithms domain.
Constraints
- 1 <= n <= 2 * 10^5
- p is a permutation of the integers 1..n
- An O(n) or O(n log n) solution is expected
Examples
Input: (5, [4, 1, 3, 2, 5])
Expected Output: "10111"
Explanation: For k=1, the value 1 appears alone, so it is balanced. For k=2, positions of 1 and 2 are not adjacent. For k=3, values {1,2,3} occupy positions 2..4. For k=4 and k=5, the required values also occupy a contiguous block.
Input: (1, [1])
Expected Output: "1"
Explanation: The only possible k is 1, and the single-element subarray [1] matches {1}.
Hints
- Instead of checking every subarray, first record the position of each value from 1 to n.
- For a fixed k, the numbers 1..k can form a valid length-k subarray exactly when their minimum and maximum positions span k indices.