Implement sampling and subarray scan | Google Interview Question