Hey y’all! I got another solution up for Leetcode 560 Subarray Sum Equals K as well as 2 very common variants Meta asks.
NOTE: I’ve never asked this before but to deeply understand the intuition behind the solution (for both the OG and its variants), please watch the whole video. Even though this is considered a Leetcode medium, it’s way harder than it seems. Tbh not a lot of other resources on this problem explain it well, and at the end of the day, if you get this in an interview, you have to be able to explain how you arrive at the solution (can’t just memorize the code, sadly).
The first variant trivially mirrors the original Leetcode problem: what if you had to return a boolean whether you’ve found at least one subarray sum that equals K? Among other things, this has implications on the data structure.
IMO the second variant is the “true” variant where the Meta interviewer may restrict the nums array to only positive numbers. A Prefix + Map solution still works but I’ll go on a limb and say it’s never accepted. Instead, the implementation from the OG problem changes entirely.
Reference to the OG LC problem: https://leetcode.com/problems/subarray-sum-equals-k/
Good luck on your phone screens & onsite loops!