Microsoft | Phone | SDE 2 | Subsequence Max Product

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-02-03Added By Anonymous
Source: Link

1 hour scheduled call with senior dev on a team (MS Teams call, no video). Couple minutes of introductions, then straight to the Codility problem


•Given a large number of upto 100 digits, the objective is to find the subsequence of n digits with max product


ex: num = 145623 , n = 3


answer would be 120, the product of 4 x 5 x 6


I quickly came up with a O(n2) solution like this:

1 2for(int i = 0; i < nums.Length - 1; i++) { 3 int product = nums[i]; 4 for(int k = 1; k < n; k++) { 5 product = product * nums[k]; 6 } 7}

This solution passed test cases, but then the interviewer pushed me to find a solution with faster time complexity. I mentioned that a sliding window could be used here instead, but through my anxiety it took me longer than expected to type out a sliding window solution. Eventually got it all working, but had lots of issues with zeroes in the num string.

Comments