Implement sampling and subarray scan
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates probabilistic sampling and random-number transformation concepts for generating uniform points in a continuous domain, alongside array-processing and algorithmic analysis skills for identifying the longest strictly increasing contiguous subarray.
Part 1: Generate a Uniform 2D Point in a Square
Constraints
- 1 <= len(rand_values) <= 10^5
- 0.0 <= rand_values[i] <= 1.0
- At least two values in rand_values are strictly between 0 and 1
- The values represent independent samples from rand01()
Examples
Input: ([0.25, 0.75],)
Expected Output: (-0.5, 0.5)
Explanation: x = 2 * 0.25 - 1 = -0.5, y = 2 * 0.75 - 1 = 0.5.
Input: ([0.5, 0.5],)
Expected Output: (0.0, 0.0)
Explanation: Both samples map to the center of the square.
Input: ([0.0, 1.0, 0.125, 0.875],)
Expected Output: (-0.75, 0.75)
Explanation: The endpoint values 0.0 and 1.0 are ignored; 0.125 and 0.875 are used.
Input: ([0.75, 0.25, 0.5],)
Expected Output: (0.5, -0.5)
Explanation: Only the first two valid samples are needed; later samples are ignored.
Hints
- A linear transformation maps Uniform(0, 1) to Uniform(-1, 1).
- To sample a point uniformly from a square, sample each coordinate independently.
Part 2: Longest Strictly Increasing Contiguous Subarray
Constraints
- 0 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- The subarray must be contiguous
- The increase must be strict, so equal adjacent values break the run
Examples
Input: ([],)
Expected Output: 0
Explanation: The empty array has no subarrays.
Input: ([7],)
Expected Output: 1
Explanation: A single element is an increasing contiguous subarray of length 1.
Input: ([1, 3, 5, 4, 7],)
Expected Output: 3
Explanation: The longest strictly increasing contiguous subarray is [1, 3, 5].
Input: ([2, 2, 2],)
Expected Output: 1
Explanation: Equal adjacent values are not strictly increasing, so each run has length 1.
Input: ([5, 1, 2, 3, 0, 1, 2, 3, 4],)
Expected Output: 5
Explanation: The longest strictly increasing contiguous subarray is [0, 1, 2, 3, 4].
Hints
- Scan from left to right and compare each element with the previous one.
- Maintain the length of the current increasing run and the best length seen so far.