The first coding round was similar to what people describe online — it's the one where you treat it as a stock ticker + date + price log. The interviewer guides you step by step, so you can pretty naturally arrive at the solution. Here's roughly what my code looked like:
from collections import defaultdict
class StockSystem(object):
def init(self, prices_data):
self.dict = defaultdict(dict) # tick: {date: price}
for p in prices_data:
self.dict[p[0]][p[1]] = (p[2], False) # (price, updated: boolean)
def get(self, date, stock='SQ'):
if date not in self.dict[stock]:
return None
(price, updated) = self.dict[stock][date]
if not updated:
return str(price)
return str(price) + " (updated)"
def update(self, date, stock, price): # additional flag to indicate if it's updating existing value or creating new entry
if stock not in self.dict:
self.dict[stock][date] = (price, False)
elif date not in self.dict[stock]:
self.dict[stock][date] = (price, False)
else:
self.dict[stock][date] = (price, True)
def percentage_change(self, start_date, end_date, tick="SQ"):
if tick not in self.dict and start_date not in self.dict[tick] or end_date not in self.dict[tick]:
return None
start_price = self.dict[tick][start_date][0]
end_price = self.dict[tick][end_date][0]
change = ((end_price - start_price) * 1.0 / start_price) * 100
formatted_change = "{:.2f}".format(abs(change))
sign = "+" if end_price >= start_price else "-"
return sign + " " + formatted_change + "%"
def percentage_change_two_stocks(self, start_date, end_date, tick1, tick2):
return self.percentage_change(start_date, end_date, tick1) + " " + self.percentage_change(start_date, end_date, tick2)
System design: the classic hotel reservation system design that shows up online a lot. Though there was a twist — the interviewer asked me not to model room type, but to model room_number directly. I ended up overcomplicating some of the edge cases there, so I'm guessing I failed this round. At the end there was a followup: how do you help different users avoid conflicts when they search and find the same room at the same time? My answer was that besides doing locking at the SQL DB level, you could additionally use a NoSQL datastore to keep track of whether a room is being held in an in-memory store, so queries would be faster, and you could show in real time on the webpage that a room is currently being held.
Second coding round: I think I'd also seen this one online. It was a calendar and event system, but I didn't really understand what was being asked at first — though it turned out fine. It was also step by step. Roughly the questions were:
- support insert() and make sure events() prints out a sorted list
- support delete() with a specific event name as the param
- support event pagination with pageSize and pageNum params (I mentioned on my own that if you can't load the whole list into memory, you'd need to load data a bit at a time until you reach the right offset)
- support checking an event's intersection with an interval (similar to the LeetCode check-interval problem)
Project deepdive: they asked about my backend project. I hadn't prepared for it that well, so some parts I didn't answer great.
HM interview: they asked about the project I was most proud of personally, plus some standard behavioral questions.
Discussion
Loading comments…