My friend Deepak gave me this Basic Requirements:
- N-level building. You can assume 5 for now
- Each level’s lift lobby has up/down buttons
- Inside each lift there are N buttons for the N target floors
- Any time, system can receive requests from any button
My basic design is not optimized for efficiency. The number of pending requests will stay below 20 since there are only that many buttons, so we iterate over all requests frequently.
My design effort is heavily focused on data structure — The more complex the requirements, the more I need to focus on clean, concise, sound data structure. They may not be necessary — a less optimal data structure can also work, but an optimal data structure helps us tremendously to cope with the complexity. I feel this problem is tractable once the data structures take shape.
Q: what if lift is in motion towards some target when a lift-lobby button is pressed and it happens to be serviceable?
A: Like the pencil solution to the space-pen challenge, my endless loop in main() may qualify as a simple solution to this daunting challenge. The system wakes up frequently to check for new requests. When sleeping, it ignores all inputs.
This simple design, if viable, avoids asynchronous or multi-threading complexities. http://pubs.vmware.com/foundry1/pg/wwhelp/wwhimpl/common/html/wwhelp.htm?context=pg&file=Foundry_PG_concepts.3.30.html is a similar single-threaded design
For simplicity, I assume the new requests from all buttons show up in some buffer (or database), so we can poll it to see them. There’s
no interrupt or callback.
Q: in your design, there are “targets” set by in-lift passengers vs. up/down requests (from lift lobbies) assigned by the system. How do you prioritize between the two types?
A: In general, targets are at higher priority than assignments, but if lift is already moving down towards Level 2 for an assigned request, it will move down all the way till that level.
I don’t want to spend too much time on any module since the correct emphasis/focus could be different in a real world design or design interview.