Prepin
Log in

Given an array of integers, display all subarrays that have a sum of 0.

Approach / Explanation

In this inquiry, the interviewer is requesting that you devise an algorithm for a particular task. You will be provided with an array of integers. For clarification, an array is a data structure that encompasses a collection of elements. In this case, the elements are integers, which can be either positive or negative. The objective is to identify all subarrays within the provided array that have a sum of zero. A subarray refers to a contiguous segment of the array. The phrase "sum of 0" indicates that the total of all elements within the subarray must equal zero when added together. To tackle this question, you might utilize the concept of prefix sums along with a Hashmap data structure. The essential points to consider in addressing the question are: Comprehend the concept of prefix sums and how to leverage a Hashmap to keep track of the prefix sum as the key and the corresponding list of indices as values. Implement the prefix sum and Hashmap approach to solve the problem.

Suggested Answer

A method to tackle this issue involves calculating the prefix sum first. As you loop through the array, you continuously add the numbers to obtain the current sum, which represents the prefix sum. Additionally, you keep a hashmap where the prefix sum serves as the key and the value is a list of indices at which this prefix sum occurs. For each new prefix sum calculated, you verify if it already exists as a key in your hashmap. If it does, it indicates that there is a subarray with a sum of zero from each index stored in the hashmap to the current index. Here is a Python example: def print_zero_sum_subarrays ( arr ) : sum_hash = { } curr_sum = 0 for i in range ( len ( arr ) ) : curr_sum += arr [ i ] if curr_sum == 0 : print ( f"Subarray from 0 to { i } " ) elif curr_sum in sum_hash : for start_index in sum_hash [ curr_sum ] : print ( f"Subarray from { start_index + 1 } to { i } " ) sum_hash . setdefault ( curr_sum , [ ] ) . append ( i )

Alternative Answer

In certain situations, there may be more efficient alternatives that require fewer operations. However, the main concept is to utilize prefix sums and hashmaps as illustrated in the earlier example. Employing effective data structures can significantly improve performance. For instance, in Java, an ArrayList in conjunction with a HashMap can be utilized for this purpose. Below is a sample Java code: void printZeroSumSubarrays ( int arr [ ] ) { HashMap < Integer , ArrayList < Integer > > map = new HashMap < > ( ) ; int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) { System . out . println ( "Subarray from 0 to " + i ) ; } ArrayList < Integer > al = new ArrayList < > ( ) ; if ( map . containsKey ( sum ) ) { al = map . get ( sum ) ; for ( int it = 0 ; it < al . size ( ) ; it ++ ) { System . out . println ( "Subarray from " + ( al . get ( it ) + 1 ) + " to " + + i ) ; } } al . add ( i ) ; map . put ( sum , al ) ; } } In this example, an ArrayList is paired with the HashMap, where the key represents the prefix sum and the value corresponds to the list of indices at which this prefix sum occurs. This approach has a time complexity of O(n) since we only iterate through the array once. The space complexity is also O(n) due to the additional space utilized (HashMap and ArrayList).

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.