Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Last active March 30, 2017 05:55
Show Gist options
  • Save amcgregor/223f4b26a127b0c49fc5f2f52f878534 to your computer and use it in GitHub Desktop.
Save amcgregor/223f4b26a127b0c49fc5f2f52f878534 to your computer and use it in GitHub Desktop.
A thought experiment job interview question / session in assistance of a user on IRC seeking mock interview advice. WIP cleanup, see previous commits.

Ask me your questions, gatekeeper, I am not afraid.

Given the opening and closing times for a store, how would you store that data, then subsequently query it, so as to answer the question: at time X (for example, now,) is the store open or closed?

What are the times?

Open 9:30am Monday, closes 5:30pm Monday. Tuesday through Friday open from 7am until 11pm. Saturday open from 11am to 4pm. Sunday closed.

:scribbles that on the whiteboard:

Talk about your thought process as you work on the problem, I'll answer any questions you have. Don't worry too much about syntax or specifics, just about breaking down the problem.

"""Identify the current state of a storefront.
Construct a sorted list of state transitions,
bisect to determine insertion point,
the element to the left of the insertion point identifies the state for the given time.
"""
from bisect import bisect_left
from datetime import datetime
# DOTW starts with 0=Monday
# all times are local
TIMES = [
(0, 9 * 60 + 30, 'open'),
(0, 17 * 60 + 30, 'closed'),
(1, 7 * 60, 'open'), # repeated data inefficiency
(1, 23 * 60, 'closed'),
(2, 7 * 60, 'open'), # repeated data inefficiency
(2, 23 * 60, 'closed'),
(3, 7 * 60, 'open'), # repeated data inefficiency
(3, 23 * 60, 'closed'),
(4, 7 * 60, 'open'), # repeated data inefficiency
(4, 23 * 60, 'closed'),
(5, 7 * 60, 'open'), # repeated data inefficiency
(5, 23 * 60, 'closed'),
(6, 11 * 60, 'open'),
(6, 16 * 60, 'closed'),
]
now = datetime.now()
now = (now.weekday(), now.hour * 60 + now.minute)
status = TIMES[bisect_left(TIMES, now)][2]
print(status)

Given your initial solution, how would you scale this to 12,000 stores with varying irregular schedules and exceptions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment