Round 3: Managerial Round
Questions: Write a function which takes 2 inputs - array of integers and a target. It should output a list of tuples that adds to target.
output = [] def targetSum(arr,target,ind,currSum,templist,output): if ind>=len(arr): return if currSum>target: return if currSum==target: output.append(tuple(templist)) return templist.append(arr[ind]) targetSum(arr,target,ind+1,currSum+arr[ind],templist,output) templist.pop() targetSum(arr,target,ind+1,currSum,templist,output) targetSum([ 3, 5, 7, 2, 6, 4, 0 ],9,0,0,[],output) print(output)
Input: ([ 3, 5, 7, 2, 6, 4, 0 ], target : 9)
Output: [(3, 2, 4), (3, 6), (5, 4), (7, 2)]
The interviewer mentioned that I did not understand the question and provided his expected output [(3, 6), (5, 4), (7, 2)], stating that it did not match my output. He then asked if I had any questions and left the call.
Later, I found that the question was actually about finding whether there is a pair of elements in the array whose sum equals the target, which is a variation of the 2Sum problem. The interviewer did not specify that the tuple should consist of pairs or have a length of 2.
Candidate's Approach
I implemented a recursive function to find all combinations of elements that sum to the target. I maintained a temporary list to store the current combination and an output list to store valid combinations. However, I misunderstood the requirement for the output format, as I returned all combinations instead of just pairs.
Interviewer's Feedback
The interviewer indicated that I did not understand the question correctly and that my output did not match the expected result. He did not provide further feedback or suggestions for improvement.