Company : Google
Location : Bangalore, India
Date of Interview : July 2nd, 2021
Telephoonic Phone Interview (45 min)
I was interviewed by a SWE from Google London.
Assume you're given the list of RequestType sent to the server on the previous day, implement the method which returns a random RequestType which has similar occurence to that of the list.
enum class RequestType { 1, 2 ,3 4 }
val observedRequest = List<RequestType>()
fun getRequestType() : RequestType {
TODO()
}For Example,
Input:
observedRequest = [1,2,1,3,1,3]
Output:
requests = [1,2,1] or [1,1,3,3] or [1,2,2,1,3,3,1,3,3,1]
Explanation:
Accumulating the output from the getRequestType() should have similar occurences as that observedRequests.
Here the frequency of RequestType are 1 = 1/3 , 2 = 1/6 , 3 = 1/3Interview Experience:
I myself was confused with the question as I initially thought that this is similar to occurrences problem in LC. I suggested him I would convert the list to map -> [RequestType = Occurrence]. And then select the entries from map and then decrement the value.
The interviewer wanted the RequestType to be generated randomly which confused me further. I asked questions back to clarify myself but it rather confused me a lot. He then finally asked have you come across Random Generator in Java and how does it work. Asked what if you select a random value from list everytime. It satisfies the condition right? Can you now implement the method?
fun getRequestType() : RequestType{
val random = Random()
return observedRequest[random.nextInt()]
}He then asked what if the random number exceeds the size and he himself corrected the code.
fun getRequestType() : RequestType{
val random = Random()
return observedRequest[random.nextInt(observedRequest.size)]
}Then he said the above implementation same as
fun getRequestType() : RequestType{
return observedRequest.random()
}I told him I wasn;t sure whether I could use the in-built APIs. He added a follow up question as what if the list size is larger; say it's in millions or billions.
I then proposed we could makes use of the map and then select a random from it. He told ok. By then the time was over. He asked whether I have any questions.
I'm still waiting for the feedback. I think mostly I won't be able to make it for the onsite round. Or are there any chances?