Google ng code interview experience
Summary
Had a Google ng code interview experience where I faced a classic problem on subarray sum equals K, showcasing the importance of understanding prefix sums and hash maps for optimization.
Full Experience
The interviewer started off very chill — after a bit of small talk, he jumped straight into the questions. The first one was a classic high-frequency problem: Subarray Sum Equals K. Given an integer array, find the number of continuous subarrays whose sum equals K. The moment you hear this question, you know they’re not testing algorithms per se — they’re checking whether you can start with a brute-force solution and progressively optimize it. The student first mentioned the brute-force approach with O(n²) complexity. The interviewer smiled, nodded, and then asked: "if I asked you to optimize it to O(n), how would you think about it?" At that moment, our voice-assist system hinted: "Explain the prefix-sum + hashmap idea, don’t rush into coding yet." So the student naturally followed up with: "I’d use a hash map to record how many times each prefix sum has appeared. While iterating, I check whether prefixSum – K exists — that tells me how than subarrays ending at the current index sum to K." He then explained and wrote the solution step by step, even adding the important detail of initializing the map with {0: 1} to correctly handle cases where the subarray from index 0 already equals K. The interviewer immediately responded: "Good, that’s the reasoning I’m looking for."