Adobe Interview Question for Data Scientists

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
of 0 vote

import random

class RandomQuoter:
    def __init__(self, quotes):
        # assume quotes are in a list
        self.values = []
        self._last_index = -1
    def add_quotes(self, quotes):
        for quote in quotes:
            self._last_index += 1
    def __len__(self):
        return self._last_index + 1
    def remove_quote(self, idx):
        if idx != self._last_index:
            last_quote = self.values[self._last_index]
            self.values[idx] = last_quote
        self._last_index -= 1
    def get_rand_quote(self):
        if len(self) == 0:
            raise ValueError('no more quotes remaining')
        idx = random.randint(0, self._last_index)
        res = self.values[idx]
        return res
sample_quotes = [3,8,1,3,59,103]
quoter = RandomQuoter(sample_quotes)
for _ in sample_quotes:
    print('randomly selected quote: {}'.format(quoter.get_rand_quote()))

- cabbagesoup June 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

The tricky part of this problem is to make random() O(1). To achieve this, use a set/hashmap to achieve O(1) insertions/deletions. To obtain O(1) for random, use an auxiliary list and return a random index from the list. Another tricky part is to keep the list in sync with the hashmap/set. To achieve this swap the quote with the last element and then remove the element when you select the random() quote.

I have provided a toy example which represents this idea:

Solution in Python Below:

from random import randint

class QuoteManager:
    def __init__(self):
        self.quoteToIndicesMap = {}
        self.quotes = []

    def add(self, quote):
        if quote not in self.quoteToIndicesMap:
            # Add it
            self.quoteToIndicesMap[quote] = len(self.quotes) - 1 # index
            return True
        return False # Already there return False

    def random(self):
        if len(self.quotes) == 0:
            print('All quotes are exhausted!')
            return 'INVALID!!'

        # Key trick is to swap with last element
        # and then delete it

        # Swap it with last position
        lastQuote = self.quotes[-1]
        randomIndex = randint(0, len(self.quotes) - 1)
        self.quotes[randomIndex], self.quotes[-1] = self.quotes[-1], self.quotes[randomIndex]

        # Delete it from list and the hashmap
        quoteToPop = self.quotes.pop()
        self.quoteToIndicesMap[lastQuote] = randomIndex
        del self.quoteToIndicesMap[quoteToPop]
        # Return the random quote
        return quoteToPop

Test code:

# Test code
q = QuoteManager()
print(q.add('Quote 1')) # True
print(q.add('Quote 2')) # True
print(q.add('Quote 3')) # True
print(q.add('Quote 4')) # True
print(q.add('Quote 1')) # False
print(q.random()) # Quote 2
print(q.random()) # Quote 4
print(q.random()) # Quote 3
print(q.random()) # Quote 1
print(q.random()) # All quotes are exhausted! INVALID!

- prudent_programmer March 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

Load into a simple array.
Use a shuffle like, Fisher-Yates shuffle. Every time you swap an element, print/return it. Keep track of last shuffled index.
While dumping the data, save the last shuffled index along the array.

- Basmati March 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

The question asks for O(1) access to a random element, you are allowed to preprocess. To pick a random element n O(1) you need to know how many quotes there are, create a random number and access in O(1). If the quotes do not change, you traverse once and store in a second file (the index file) the offset to the quote. You can read the index into memory or random access it (fixed record length). If you do not want to self create an index use a dbms. If you need to maintain the index after adding/deleting quotes, use a btree. Of the stuff doesn't fit on a single machine, shard.

- Chris March 27, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More