Skip to main content
All Questions

A C++ function should count the contiguous subarrays whose sum equals a target using a prefix-sum frequency map, but it has several bugs. Find and fix them with minimal changes.

Unlock detailed company stats for this questionUpgrade
A C++ function should count the contiguous subarrays of an integer array whose sum equals a target (the answer fits in an int). It keeps a hash map from prefix sum to frequency, which starts with the entry 0 mapped to 0. For each element it adds the element to the running sum, increments the map entry for the running sum, and then, if the map contains the running sum minus the target, sets the result to that entry's frequency. Find the bugs and fix them with minimal changes.

Community answers

No answers contributed by the community yet.

Related courses

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.