Count Zero-Sum Fragments
Updated
Problem statement
Write a function solution that, given an array A consisting of N integers, returns the number of fragments of A whose sum equals 0 (that is, pairs (P, Q) such that P <= Q and the sum A[P] + A[P+1] + ... + A[Q] is 0). The function should return -1 if this number exceeds 1,000,000,000.
For example, given A = [0, 0, ..., 0] of length 100,000, the function should return -1.
Function
solution(A: int[]) → intExamples
Example 1
A = [2, -2, 3, 0, 4, -7]return = 4
The zero-sum fragments are:
[2, -2][0][3, 0, 4, -7][2, -2, 3, 0, 4, -7]
Constraints
- Write an efficient algorithm for the following assumptions:
Nis an integer within the range[1..100,000].- Each element of array
Ais an integer within the range[-10,000..10,000].
More Tesla interview questions
- Maximum Insertions Without Three Consecutive AOA · Seen Jun 2026
- Maximum Number of Workers with Equal Left and Right ShoesOA · Seen Mar 2025
- Maximum Even-Sum Adjacent Pairs in a Circular ArrayOA · Seen Oct 2021
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- Alternate Positive and Negative ValuesONSITE INTERVIEW · Seen Aug 2026
- Enumerate Character Paths to a MarkerONSITE INTERVIEW · Seen Aug 2026
- Shared Counter Snapshot SumONSITE INTERVIEW · Seen Aug 2026
- Merge JSON Array FieldsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Count Stable Server SubsegmentsScale AI · OAScale AI interview questions
- Number of Unique Elements After Modifications 🍁Infosys · OAInfosys interview questions
- Count Paired Subarrays Reaching a TargetZipRecruiter · OAZipRecruiter interview questions
- Count Subarrays with Sum KIndeed · OAIndeed interview questions
- Count Subarrays With Given XORInMobi · OAInMobi interview questions
- Count Subarrays with Sum Divisible by KVisa · OAVisa interview questions
- Beautiful SubarraysAtlassian · OAAtlassian interview questions
- Find Security LevelAmazon · OA
- Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
Tesla interview experiences
- Tesla Experienced Infrastructure SWE Interviews: 1 Experience
- Tesla Older SWE Intern Reports: 14 Experiences (Part 4 of 4)
- Tesla Embedded and SRE Intern Loops: 15 Experiences (Part 3 of 4)
- Tesla SWE and Data Intern Rounds: 10 Experiences (Part 2 of 4)
- Tesla Intern Python Interview Prep Guide
- 2017–2026 Tesla Intern OA / Interviews: 83 Experiences (Part 2 :)