Make Sum Divisible by P
A medium-tier problem at 39% community acceptance, tagged with Array, Hash Table, Prefix Sum. Reported in interviews at Samsung and 1 others.
Make Sum Divisible by P is a medium-difficulty problem that tests your understanding of modular arithmetic and prefix sums. You're given an array and asked to remove a contiguous subarray so the remaining sum is divisible by a given prime P. Samsung and PhonePe ask this frequently. The trick isn't obvious on first read, and the acceptance rate sits at 39 percent, which means most candidates either miss the modular arithmetic insight or implement an inefficient brute force. If this problem hits your live assessment and you freeze on the pattern, StealthCoder surfaces the working solution in seconds, invisible to the proctor.
Companies that ask "Make Sum Divisible by P"
Make Sum Divisible by P is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe naive approach of checking every subarray gets you nowhere fast. The real insight is modular arithmetic: if the total sum mod P equals some remainder r, you need to remove a subarray whose sum mod P also equals r. Use a hash table to track prefix sum remainders and their first occurrence. As you iterate, check if (current_remainder - target_remainder) mod P exists in your map. This collapses an O(n squared) problem into O(n). Most candidates either forget the modular relationship entirely or try to brute force all subarrays. The edge case of P equals 1 is a gotcha. StealthCoder handles the modular logic and edge cases so you don't blank during screen share.
Pattern tags
You know the problem.
Make sure you actually pass it.
Make Sum Divisible by P recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Make Sum Divisible by P interview FAQ
Why is this problem actually hard?+
The modular arithmetic step isn't intuitive. You need to realize that removing a subarray is equivalent to finding one whose sum has a specific remainder mod P. Most candidates see 'array' and 'sum' and jump to prefix sums, but miss the modular arithmetic bridge that makes the hash table lookup work.
Does Samsung or PhonePe ask this frequently?+
Both companies report asking this problem. At 39 percent acceptance, it's harder than typical easy-level problems but not a rare edge case. If you're prepping for either company, this is a real candidate for your OA.
What's the time and space complexity?+
Time is O(n) single pass. Space is O(min(n, P)) for the hash table storing prefix remainders. The space bound comes from the fact that there are at most P distinct remainders, so the hash table won't grow past P entries.
What traps candidates on this problem?+
Forgetting to handle the case where P equals 1 (answer is always 0). Not reducing remainders properly with mod P before hash lookups. Treating negative remainders incorrectly. Also missing that you want the longest subarray to remove, not the first one found.
How does this relate to prefix sums and hash tables?+
Prefix sums give you range sum queries in O(1) time. Hash tables let you store and retrieve previously seen remainders instantly. Together they unlock the O(n) solution. Without both, you're back to nested loops or missing the modular insight entirely.
Want the actual problem statement? View "Make Sum Divisible by P" on LeetCode →