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.
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.
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.