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.

Pages: 1 2

8 Responses to “An Impossible Interview Question”

  1. Paul said

    In Python.

    from random import randrange
    from collections import Counter
    def random_char(seq):
        result = None
        for i, c in enumerate(seq, 1):
            if randrange(i) == 0:
                result = c
        return result
    test = (random_char("abcd") for i in range(1000000))
    # [('a', 250848), ('d', 250520), ('b', 249523), ('c', 249109)]
  2. Zack said

    @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.

  3. nims11 said

    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?

  4. nims11 said

    My bad, the probabilities are indeed equal

  5. Paul said

    @Zack. Thank you. You make me blush.

  6. V said

    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.

    def pick_random(stream)
    # test
    input = "an impossible interview question"
    puts 1_000_000.times
      .map { pick_random(input) }
      .sort_by {|k, v| k }
      .map {|k, v| "#{k}: #{v.count}" }

    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

  7. Daniel said

    Here’s a solution in Java.

    There’s a Wikipedia page on Reservoir Sampling:

    import java.util.Random;
    public class ReservoirSampling {
        private static Random rng = new Random();
        // a stream of characters with unknown length.
        public static class Stream {
            // the unknown remaining size of the stream. (stream size starts between 1 and 5000)
            private int n = rng.nextInt(5000) + 1;
            private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
            public boolean hasNext() {
                return n > 0;
            // get the next character in the stream (or null if stream is empty)
            public Character next() {
                if (n > 0) {
                    return alphabet.charAt(rng.nextInt(alphabet.length()));
                } else {
                    return null;
        public static Character sample(Stream stream) {
            int i = 1;
            Character c =;
            for (Character next =; next != null; next = {
                if (rng.nextDouble() < 1.0/i) {
                    c = next;
            return c;
        public static void main(String[] args) {
            System.out.println("Sample: " + sample(new Stream()));


    Sample: e
  8. Jussi Piitulainen said

    There was a Praxis exercise on Reservoir Sampling a while back.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: