An Impossible Interview Question
October 18, 2016
I think the desired solution was Floyd’s algorithm: Pick the first character with probability 1. Then replace it with the nth character in the input with probability 1/n. When the stream is exhausted, output the character that remains:
(define (floyd xs) (let loop ((xs xs) (n 1) (x #f)) (if (null? xs) x (loop (cdr xs) (+ n 1) (if (< (rand) (/ n)) (car xs) x))))) > (floyd '(1 2 3 4 5)) 4
We take the input all at once, because Scheme handles lists better than streams, but we don’t make use of anything other than the current character and the count of characters that we have seen, so we do properly solve the problem. You can run the solution at http://ideone.com/Hh4IfH.
Except, of course, that we don’t properly solve the problem because we use a counter n as well as a character. I don’t see any way to solve the problem as given. Do you?
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.)