Google's New Grad coding rounds love to wrap a classic pattern, and this "subarray sum mod equals k" is textbook: the prompt looks roundabout, but at its core it's prefix-sum remainders plus a hashset. Based on an oavoservice student's Google NG debrief, this post walks the problem from brute force to optimal. At the end you'll find VO assist / VO proxy paths for anyone in a new-grad, career-switch, or backend job search.
1. Google NG Coding Round Overview
| Dimension | Details |
|---|---|
| Format | Remote video, ~45 min per round |
| Volume | 1 coding question + follow-ups |
| Language | Your choice; Python / Java / C++ common |
| Focus | Prefix sum / hashing, complexity optimization, edge cases, communication |
| Style | Classic-problem variant—testing whether you spot the underlying pattern |
Google NG questions are mostly LeetCode Medium. The point isn't novelty but whether you quickly recognize "which classic pattern is this a variant of" and implement it cleanly.
2. Problem
Given a positive-integer array nums and an integer k, write a function that decides whether nums contains a contiguous non-empty subarray whose element sum, taken mod 6,000,009, equals k.
3. Framing: mod + subarray sum → prefix-sum remainder
"Contiguous subarray sum" should immediately suggest prefix sums: the sum of (i, j] is prefix[j] - prefix[i]. We want that difference, mod M = 6_000_009, to equal k:
(prefix[j] - prefix[i]) % M == k
Rearranged: prefix[j] % M and prefix[i] % M differ by k (mod M). So as you scan, drop each seen prefix-sum remainder into a hashset; for the current remainder cur, if the set contains (cur - k) % M, a valid subarray exists.
Naive approach (for contrast)
Enumerate all (i, j) ranges, sum and mod—O(n²) or worse. It times out on large inputs and is only useful to validate small cases.
Optimal: prefix-sum remainder + hashset
Note the subarray is non-empty: process the current element and update cur first, then check; and seed the set with remainder 0 (the empty prefix) to cover subarrays starting at index 0.
def HasSubarrayKMod(nums, k):
M = 6_000_009
k %= M
seen = {0} # empty-prefix remainder, covers subarrays from index 0
cur = 0
for x in nums:
cur = (cur + x) % M
if (cur - k) % M in seen:
return True
seen.add(cur)
return False
print(HasSubarrayKMod([2, 3, 5], 8)) # True, subarray [3,5] sums to 8
print(HasSubarrayKMod([2, 3, 5], 100)) # False
Time complexity: O(n) (single pass, amortized O(1) hashing). Space complexity: O(n) (up to n+1 remainders).
4. On-the-Spot Reminders
- For "mod + subarray sum," reach straight for prefix-sum remainders + a hashset—it's the standard solution for this class.
- Seed the set with remainder 0 (the empty prefix) first, or you'll miss the "subarray starts at index 0" edge case.
- Take
kmod M too, so akoutside[0, M)doesn't break the check. - State "why it's a difference of prefix sums" verbally before coding, giving the interviewer a clear reasoning trail.
FAQ
Q1: Why use prefix-sum remainders here?
Because a subarray sum is the difference of two prefix sums, and adding the mod turns it into "do two prefix-sum remainders differ by k (mod M)." A single pass storing seen remainders in a hashset decides it in O(n)—far better than enumerating all subarrays at O(n²).
Q2: Why seed the set with a 0?
0 is the remainder of the "empty prefix." It covers the case where the subarray starts exactly at index 0—there the current prefix-sum remainder equals k directly, and you need the empty prefix to make the difference work out.
Q3: Does the odd modulus 6,000,009 matter?
No—the algorithm holds for any positive modulus M. The specific value only affects the remainder range, not the prefix-sum-remainder + hashset idea.
Q4: How do I prepare efficiently for Google NG coding rounds?
Drill by topic—prefix sum / hashing, two pointers, graph and tree traversal, DP—timed and narrating your reasoning. For timed mocks by Google NG question type, line-by-line walkthroughs, or full VO assist / VO proxy support, see the path below.
Preparing for Google NG interviews?
Google NG coding rounds love classic-problem variants—what they reward is pattern recognition and clear communication. oavoservice offers full Google prep: timed mocks on prefix sum / hashing / graph and tree traversal / DP high-frequency questions, framing and verbal-clarity polishing, and predictions by NG question type. Coaches include senior big-tech engineers, with full VO assist / VO proxy support.
Add WeChat Coding0201 now to get Google questions and coaching.
Contact
- WeChat: Coding0201
- Email: [email protected]
- Telegram: @OAVOProxy