YOE: 6 years
I was contacted by the recruiter regarding an oppurtunity with Google Poland and was scheduled a phone screen round.
Screening Round:
Q: Given a array of stock prices, find the max stock prices for the given second range.
Example Input
prices: [17,20,19,4,9,10,12,24], second: 3
second will range from 1 to length of the array.
Output
[17,20,20,20,19,10,12,24]
Explanation:
Intially we have only one second value - so 17 is the greater
Then among 17 and 20 - 20 is greater
Now we have three second stock price - among 17 20 19 - 20 is greater
Now we need to remove 17 and add 4, since the range is only 3 second - among 20 19 4 - 20 is again greater
Similarly 19 4 9 - 19 is greater
4 9 10 - 10 ,
9 10 12 - 12 ,
10 12 24 - 24
My approach:
I was able to explain a solution using heaps and two pointer which interviewer seemed ok with but when coding is where the problem began.
Since javascript dint have any heaps datastructure by default, I checked with my interviewer, whether I can assume I have a function which will give me max at O(1) to my suprise he said no and asked me to either implement it if its not available by default or use a language which has heap. That was strange since he is ok to use it if the language provides or has to implement it ourself.
Since I had very little time, I told I will do the brute force approach and then will try to integrate the heaps approach as I dont want to start with heaps and end up not solving the problem at all. Below is the brute force code I came up with
const findMaxPerSecond = (prices, s) => {
const result = []
if (!prices.length) {
return result
}
for (let i = 0; i < prices.length; i++) {
let currentMax = Math.max()
const start = Math.max(0, i - s + 1)
for (let j = start; j <= i; j++) {
currentMax = Math.max(currentMax, prices[j])
}
result[i] = currentMax
}
return result
}I ran the above test case and it seems to work and before I was able to code up the Heap time got over.
Then I explained the time complexity of the above problem, its O(n * s), in worst case where s ~ n , its O(n * n)
Feedback:
Today I got the call from the recruiter regarding the feedback.
1.) He told I was able to properly communicate and come back with an optimal solution but was not able to code the optimal solution.
2.) He also told the brute force approach I came up with has logically bugs and even when going through the sample input I was not able to catch it.
This sounded strange, as I think the logic is correct, after the interview I even ran it and found it to be working, not sure if I missed any edge.
Or it could be the interviewer dont have experience with JS, for example, Math.max() will be -Infinity In js, that's why I was using it, may be he tought it will be +Infinity because of the function name, not exactly sure what's wrong can just speculate now
Overall, It was a good experience and I guess its time to start learning a language which supports heaps.. 😅😅