## Adobe Interview Question for Data Scientists

Country: India
Interview Type: In-Person

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

``````import random

class RandomQuoter:

def __init__(self, quotes):
# assume quotes are in a list
self.values = []
self._last_index = -1

for quote in quotes:
self._last_index += 1
self.values.append(quote)

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
self.values.pop()

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]
self.remove_quote(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()))``````

Comment hidden because of low score. Click to expand.
-1
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 = []

if quote not in self.quoteToIndicesMap:
self.quotes.append(quote)
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.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!``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

Comment hidden because of low score. Click to expand.
-1
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.

Name:

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

### Books

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

### Videos

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.