Nonzero Totals by Name
Updated
Problem statement
You are given a list of pairs. Each pair holds a name and an integer. The pairs come as two arrays of the same length, names and values: pair i is names[i] together with values[i].
Add up the integers that belong to each name. Then return one line for every name whose final sum is not zero, and leave out every name whose sum is zero. Each line is the name, one space, and the sum, such as "Suresh 30". Write the sum as a plain integer, with a minus sign when it is negative.
Put the lines in the order in which the names first appear in names. Names are case-sensitive, so "Sam" and "sam" are different names. If every sum is zero, return an empty array.
Function
nonZeroTotals(names: String[], values: int[]) → String[]Examples
Example 1
names = ["Suresh","Suresh","Aditya","Aditya"]values = [10,20,20,-20]return = ["Suresh 30"]Suresh's sum is 10 + 20 = 30. Aditya's sum is 20 + (-20) = 0, so Aditya is left out.
Example 2
names = ["Asha","Bilal","Asha","Chen","Bilal","Dev"]values = [4,-3,6,0,3,-9]return = ["Asha 10","Dev -9"]Asha's sum is 4 + 6 = 10 and Dev's sum is -9. Bilal's sum is -3 + 3 = 0 and Chen's sum is 0, so both are left out. Asha appears before Dev, so her line comes first.
Example 3
names = ["Sam","sam","Sam"]values = [7,2,-7]return = ["sam 2"]Letter case matters, so "Sam" and "sam" are different names. Sam's sum is 7 + (-7) = 0, and sam's sum is 2.
Constraints
1 <= names.length <= 10^4values.length == names.length1 <= names[i].length <= 20names[i]contains only English letters (a-zandA-Z).-10^9 <= values[i] <= 10^9
More EPAM interview questions
- Rob Houses Without Getting CaughtOA · Seen Jul 2024
- Detect a Loop in a Linked ListONSITE INTERVIEW · Seen Oct 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Oct 2026
- Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026
- Container With Most WaterPHONE SCREEN · Seen Oct 2026
- Convert an Entry List to a MapPHONE SCREEN · Seen Oct 2026
- Count Array Element FrequenciesPHONE SCREEN · Seen Oct 2026
- Find First and Last Position of Element in Sorted ArrayPHONE SCREEN · Seen Oct 2026
- Group AnagramsPHONE SCREEN · Seen Oct 2026
- Longest Consecutive SequencePHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Customer Signer ScreeningState Farm · OA
- String-Pair Frequency SimilarityIBM · OAIBM interview questions
- Number-Character Bijective MappingAdobe · OAAdobe interview questions
- Unique Difference PatternMicrosoft · OAMicrosoft interview questions
- Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
- Palindrome-Rearrangeable PairsExpedia · OAExpedia interview questions
- Count Sales Rung Up at the Wrong PriceDocuSign · OADocuSign interview questions