Multiple Inheritance
|
|
Examples
Hash Table
- For simplicity, are the keys integers only? (Yes)
- For collision resolution, can we use chaining? (Yes)
- Do we have to worry about load factors? (No)
- Can we assume inputs are valid or do we have to validate them? (Assume they’re valid)
- Can we assume this fits memory? (Yes)1234567891011121314151617181920212223242526272829303132333435363738class Item(object):def __init__(self, key, value):self.key = keyself.value = valueclass HashTable(object):def __init__(self, size):self.size = sizeself.table = [[] for _ in range(self.size)]def _hash_function(self, key):return key % self.sizedef set(self, key, value):hash_index = self._hash_function(key)for item in self.table[hash_index]:if item.key == key:item.value = valuereturnself.table[hash_index].append(Item(key, value))def get(self, key):hash_index = self._hash_function(key)for item in self.table[hash_index]:if item.key == key:return item.valueraise KeyError('Key not found')def remove(self, key):hash_index = self._hash_function(key)for index, item in enumerate(self.table[hash_index]):if item.key == key:del self.table[hash_index][index]returnraise KeyError('Key not found')
Parking Lot
- What types of vehicles should we support? (Motorcycle, Car, Bus)
- Does each vehicle type take up a different amount of parking spots?
Yes- Motorcycle spot -> Motorcycle
- Compact spot -> Motorcycle, Car
- Large spot -> Motorcycle, Car
- Bus can park if we have 5 consecutive “large” spots
- Does the parking lot have multiple levels? (Yes)
|
|
Call Center
- What levels of employees are in the call center? (Operator, supervisor, director)
- Can we assume operators always get the initial calls? (Yes)
- If there is no free operators or the operator can’t handle the call, does the call go to the supervisors? (Yes)
- If there is no free supervisors or the supervisor can’t handle the call, does the call go to the directors? (Yes)
- Can we assume the directors can handle all calls? (Yes)
- What happens if nobody can answer the call? (It gets queued)
- Do we need to handle ‘VIP’ calls where we put someone to the front of the line? (No)
- Can we assume inputs are valid or do we have to validate them? (Assume they’re valid)
|
|
Deck of Cards
- Is this a generic deck of cards for games like poker and black jack? (Yes, design a generic deck then extend it to black jack)
- Can we assume the deck has 52 cards (2-10, Jack, Queen, King, Ace) and 4 suits? (Yes)
- Can we assume inputs are valid or do we have to validate them? (Assume they’re valid)
|
|
Design an LRU cache¶
- What are we caching? (cahing the results of web queries)
- Can we assume inputs are valid or do we have to validate them? (Assume they’re valid)
- Can we assume this fits memory? (Yes)
|
|