Panda Guru LogoPanda
Guru

Can you help determine its time complexity? - Today's leetcode POD

Round 1

Questions: Can you help determine its time complexity? Leetcode Problem Link

class Solution: # function for sum of digits of a number def checkDigitSum(self, n): total=0 while n!=0: rem=n%10 total+=rem n=n//10 return total def maximumSum(self, nums: List[int]) -> int: n=len(nums) result=-1 d={} for i in nums: digitSum=self.checkDigitSum(i) if digitSum not in d: d[digitSum]=[i, -1] else: if d[digitSum][1] == -1: if d[digitSum][0] < i: d[digitSum][1]=i else: d[digitSum][1]=d[digitSum][0] d[digitSum][0]=i else: # if both (0 and 1) < i if i > d[digitSum][0] and i > d[digitSum][1]: d[digitSum][0]=d[digitSum][1] d[digitSum][1]=i elif i > d[digitSum][0] and i < d[digitSum][1]: d[digitSum][0]=i for key, values in d.items(): a=values[0] b=values[1] if a>-1 and b>-1: result=max(result, a+b) return result
Candidate's Approach

The candidate implemented a solution that calculates the maximum sum of a pair of numbers with equal digit sums. The approach involves:

  • A helper function checkDigitSum to compute the sum of digits of a number.
  • A dictionary to store the two largest numbers for each unique digit sum encountered.
  • Iterating through the list of numbers to populate the dictionary and then determining the maximum sum from the stored pairs.

Challenges included ensuring that the dictionary correctly maintained the two largest numbers for each digit sum.

Interviewer's Feedback

No feedback provided.