Randomly partition array into k segments
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates algorithm design, randomization techniques, and time/space complexity analysis within the Coding & Algorithms domain, focusing on partitioning an array into random non-empty segments.
Constraints
- 1 <= len(nums) <= 100000
- 1 <= k <= len(nums)
- -10^9 <= nums[i] <= 10^9
- 0 <= seed < 2^32
- Output must be k non-empty contiguous subarrays whose concatenation equals nums
Hints
- Reduce the problem to choosing k-1 distinct cut positions among n-1 gaps.
- Use a seeded PRNG to make the output deterministic for testing.
- Floyd's algorithm samples without replacement in O(k) time and O(k) space.
- After selecting cuts, sort them and slice the array accordingly.
- To test randomness quality, run many trials and apply frequency checks (e.g., chi-square) over cut positions.