A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.
Show algorithmically how you would go about doing this in as few drops as possible. (Your answer should be a number of the fewest drops needed for testing 2 eggs on 100 floors)
Best Case: 1st floor -> 1 + 1 = 2 drops total
Worst Case: 99th floor -> 10 + 9 = 19 drops total
Generalize the problem to n floors. If there are 1000 floors, then drops for #1 is the primary contributer. So, need to find solution to where number of drops for #1 + #2 is constant for all cases of a given building.
Consider:
Triangle series:
$$x + (x-1) + (x-2) + (x-3) + ... + 1$$which equals x(x+1)/2. Solve for x knowing total number of floors. So,
$$x(x+1)/2 = n$$Solve with numpy: convert to zero on one side, then: $$0 = 0.5x^2 +0.5x -n $$
In [3]:
import numpy as np
n=100
np.roots([0.5,0.5,-n])
Out[3]:
Positive root is 13.651.
So, first floor to try is 14.
Worst case is 14.
In [11]:
#Brute force, looping. (Simplest)
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
print "brute force: ",fib(7)
# Using recursion (slowest)
def fibR(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
print "recursion: ",fibR(7)
## Using memoization (Fastest). Memoize the simple loop/brute force func.
def memoize(fn, arg):
memo = {}
if arg not in memo:
memo[arg] = fn(arg)
return memo[arg]
fibm = memoize(fib,7)
print "memoization: ",fibm
Given a list of stock prices, where the index is the time stamp, find the greatest profit that can be made by a single purchase and sale.
For example, if you were given the list of stock prices:
prices = [12,11,15,3,10]
Then your function would return the maximum possible profit, which would be 7 (buying at 3 and selling at 10).
Loop over every element and find max value after it, keeping track of max profit.
Go through list, tracking:
- min price so far
- max profit so far (ie. max(current_price-min, max_profit) )
Corner cases:
Fixes:
Given a list of integers, write a function that will return a list, in which for each index the element will be the product of all the integers except for the element at that index
For example, an input of [1,2,3,4] would return [24,12,8,6] by performing [2×3×4,1×3×4,1×2×4,1×2×3]
Note: You can not use division in your answer!
In [16]:
from IPython.display import Image
Image(filename='twoStacks-oneQue.png', width=300)
Out[16]:
Linked Lists have constant-time insertions and deletions in any position, in comparison, arrays require O(n) time to do the same thing.
Linked lists can continue to expand without having to specify their size ahead of time.
Given a singly linked list, write a function which takes in the first node in a singly linked list and returns a boolean indicating if the linked list contains a "cycle".
Two markers race, one twice as fast. If they are ever equal, then there is a cycle.
Run through once and switch 'previous' and 'next' pointers.
For each node: start, temp= None
Full version
Now, end of list is front.
First there is an elevator class. It has a direction (up, down, stand, maintenance), a current floor and a list of floor requests sorted in the direction. It receives request from this elevator.
Then there is a bank. It contains the elevators and receives the requests from the floors. These are scheduled to all active elevators (not in maintenance).
The scheduling will be like:
Each elevator has a set of states.
There are additional signals:
EDIT: Some elevators don't start at bottom/first_floor esp. in case of skyscrapers.
min_floor & max_floor are two additional attributes for Elevator.
bitwise XOR is ^ in Python and many other languages. It returns binary 0 if elements are same, and it returns element when element ^ 0.
General-purpose, not integers: Merge sort, or heap sort. Worst case $O(nlogn)$
For a list of integers: Radix or bucket sort. $O(n)$
Corner cases:
Almost everyone knows the celebrity, but celebrity doesn't know anyone else.
Note: Key
In [ ]: