Implement sampling, subarray scan, and percentile estimate
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This multi-part problem evaluates skills in random sampling and geometric probability for uniform 2D sampling, algorithmic array processing for finding the longest strictly increasing contiguous subarray, and statistical approximation and interpolation from bucketed histogram counts to estimate percentiles.
Part 1: Map Uniform Random Samples to a Square
Constraints
- `0 <= u <= 1`
- `0 <= v <= 1`
- `u` and `v` should be treated as independent samples
- Your mapping must preserve uniformity over the square area
Examples
Input: (0.25, 0.75)
Expected Output: (-0.5, 0.5)
Explanation: Map each coordinate with `2*t - 1`.
Input: (0.5, 0.5)
Expected Output: (0.0, 0.0)
Explanation: The center of `[0, 1]` maps to the center of the square.
Input: (0.0, 1.0)
Expected Output: (-1.0, 1.0)
Explanation: Boundary-value edge case used for deterministic testing.
Input: (1.0, 0.0)
Expected Output: (1.0, -1.0)
Explanation: Another boundary-value edge case.
Hints
- How do you linearly map a number from `[0, 1]` into `[-1, 1]`?
- If the x-coordinate and y-coordinate are transformed independently in the same way, the 2D point remains uniform over the square.
Part 2: Longest Increasing Continuous Subarray
Constraints
- `0 <= len(nums) <= 200000`
- `-10^9 <= nums[i] <= 10^9`
- If the array is empty, return `0`
Examples
Input: ([1, 3, 5, 4, 7],)
Expected Output: 3
Explanation: The longest strictly increasing contiguous subarray is `[1, 3, 5]`.
Input: ([2, 2, 2, 2],)
Expected Output: 1
Explanation: Equal neighbors do not count as strictly increasing.
Input: ([],)
Expected Output: 0
Explanation: Edge case: empty array.
Input: ([10],)
Expected Output: 1
Explanation: Edge case: a single element is an increasing subarray of length 1.
Input: ([1, 2, 3, 4, 5],)
Expected Output: 5
Explanation: The entire array is strictly increasing.
Hints
- Scan the array from left to right and track the current increasing streak length.
- When `nums[i] <= nums[i-1]`, the increasing streak must restart at length `1`.
Part 3: Approximate a Percentile from Histogram Buckets
Constraints
- `1 <= len(buckets) <= 200000`
- `left < right` for every bucket
- `count` is a non-negative integer
- Buckets are ordered by boundary
- The total count `sum(count)` is positive
- `0 <= p <= 1`
Examples
Input: ([(0, 10, 50), (10, 20, 30), (20, 40, 20)], 0.9)
Expected Output: 30.0
Explanation: Total count is 100, target is 90, which lies halfway through the last bucket, so the estimate is `20 + 0.5 * (40 - 20) = 30.0`.
Input: ([(0, 5, 2), (5, 15, 8)], 0.5)
Expected Output: 8.75
Explanation: Total count is 10, target is 5, which lies in the second bucket. Fraction inside the bucket is `(5 - 2) / 8 = 0.375`, so estimate is `5 + 0.375 * 10 = 8.75`.
Input: ([(0, 5, 2), (5, 15, 8)], 0.0)
Expected Output: 0.0
Explanation: Edge case: the 0th percentile is the left boundary of the first positive-count bucket.
Input: ([(0, 5, 2), (5, 15, 8)], 1.0)
Expected Output: 15.0
Explanation: Edge case: the 100th percentile is the right boundary of the last positive-count bucket.
Input: ([(0, 5, 0), (5, 10, 10)], 0.25)
Expected Output: 6.25
Explanation: Zero-count buckets are skipped. The percentile lies 25% of the way through the second bucket.
Hints
- First locate which bucket contains the percentile by scanning cumulative counts.
- Once you know the correct bucket, use the fraction of the bucket's count needed to reach the target rank, then scale that fraction across the bucket width.