Amazon SDE2 Interview Experience
I had my first round of Amazon interview yesterday which was LLD round. We started off with basic intros and jumped into a few LP questions which took about 15-18 mins.
We later switched to LLD question where he asked me to design a Rate Limiter. He asked me to assume the requirements by myself. I started off with the basic requirements and explained the token bucket algorithm which would be used to design the Rate Limiter.
He asked me to code the algorithm and I did it accordingly by taking into account all the parameters. My code was something like this:
class TokenBucket:
def init(self, capacity, refill_rate):
1 self.capacity = capacity
2 self.tokens = capacity
3 self.refill_rate = refill_rate
4 self.last_refill_timestamp = time.time()
5 self.lock = threading.Lock()
6
7def refill(self):
8 now = time.time()
9 elapsed = now - self.last_refill_timestamp
10 added_tokens = elapsed * self.refill_rate
11 self.tokens = min(self.capacity, self.tokens + added_tokens)
12 self.last_refill_timestamp = now
13
14def allow_request(self, tokens_required=1):
15 with self.lock:
16 self.refill()
17 if self.tokens >= tokens_required:
18 self.tokens -= tokens_required
19 return True # Allowed
20 else:
21 return False # Rate limitedHe then asked me a follow up where if there are 10 threads running and there are 1000 API calls made, how would you handle it. I quite didn't understand this. Can someone help me out?
Also, what are the chances I might get a hire?