An Impossible Interview Question
October 18, 2016
Today’s exercise is an interview question from Amazon:
You are to write a program that reads a stream of characters from the input and returns a random character from the stream. Any character should have an equal probability of being returned. You may only use a single character of storage space; in particular, you may not save multiple characters from the input stream.
Your task is to write a program that solves the Amazon interview task. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
In Python.
@Paul. Kudos for such an elegant piece of code! I have my own implementation of this in Julia but I don’t think it’s any better than yours (though it’s probably faster due to the nature of the language). Anyway, I’m glad there are people like you who don’t take the term “impossible” very seriously. Thank you for sharing.
Doesn’t your solution have higher probabilities to return initial numbers (the question asks for equal likeliness) ? For a 3 character sequence, the probabilities would be 1/2, 2/3, 1/3 for the characters, respectively, right?
My bad, the probabilities are indeed equal
@Zack. Thank you. You make me blush.
Solution in Ruby
The tricky part of this one is the condition:
> Only 1 character of storage space; in particular, you may not save multiple characters from the input stream
What is storage? variables? ram? disk?
Anyway here is my solution which might not comply with the storage condition.
Output (How many times each letter was picked?)
: 58765
a: 58743
b: 58609
e: 58692
i: 58765
l: 58512
m: 58883
n: 58760
o: 58570
p: 59486
q: 58672
r: 59039
s: 58658
t: 58681
u: 59169
v: 58940
w: 59056
Here’s a solution in Java.
There’s a Wikipedia page on Reservoir Sampling: https://en.wikipedia.org/wiki/Reservoir_sampling
Output:
There was a Praxis exercise on Reservoir Sampling a while back.
The proposed solution stores one character, but it also stores one integer counter worth of state. Not sure it meets the requirements of this shibboleth.
The problem statement is ambiguous. Does it mean that the probability of choosing a character in the running text (a token) is equal, or that the probability of choosing a distinct character (a type) is equal? If the latter, I think the problem truly is impossible, because you must know what characters appear or do not appear.
As for the counter problem, I would weasel it as follows: you are allowed to have only one character, but that says nothing about keeping integer state in addition. (Obviously this doesn’t mean keeping the characters in ints!)
(There’s nothing inherently wrong with inherent or impossible interview questions, especially if they get the answerer to ask counter-questions, something interviewees are normally quite unwilling to do, although they need this skill for actual work.)