StartUp Interview Question for Data Scientists

Country: India
Interview Type: Written Test

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

class Cache():
	def __init__(self, cacheSize=30):
		self.item_keys = []
		self.item_values = []
		self.hits = 0
		self.misses = 0
		self.cacheSize = cacheSize
	def cacheResult(self, item_key, item_value):
		if len(self.item_keys) == self.cacheSize:
		self.item_keys.insert(0, item_key)
		self.item_values.insert(0, item_value)
	def getResult(self, item_key):
		if item_key in self.item_keys:
			self.hits += 1
			return self.item_values[self.item_keys.index(item_key)]
			self.misses += 1
			return -999
	def getHits():
		return self.hits
	def getMisses():
		return self.misses

def Demo_f(original_function, cache = Cache(cacheSize = 2)):
	def new_function(*args, **kwargs):
		value = cache.getResult(args[0])
		if(value != -999):
			#Using Cache...
			return value
			#Computing Value...
			value = original_function(*args, **kwargs)
			cache.cacheResult(args[0], value)
			return value
	return new_function

def f(n):
	if n < 2:
		return n
	return f(n-1) + f(n-2)

for x in range(10):

- udaysagar2177 October 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

def cache(f, size):
    cached_vals = {}
    cached_args = []

    def wrapped(*args):
        if args in cached_vals:
            return cached_vals[args]

        result = f(*args)

        if len(cached_args) == size:
            args_of_result_to_remove = cached_args.pop()
            del cached_vals[args_of_result_to_remove]

        cached_vals[args] = result
        cached_args.insert(0, args)

        return result

    return wrapped

- maguire.brendan October 20, 2015 | 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