Build a Weekly Calendar
Company: Robinhood
Role: Frontend Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Implement an interactive weekly calendar UI.
Requirements:
- Display a one-week calendar with 7 days.
- The calendar should be divided into 1-hour time slots.
- Each visible cell represents a specific day and hour.
- When the user clicks a cell, open a dialog that allows the user to enter an event name.
- After the user submits the dialog, show the event in the corresponding calendar cell.
- The implementation should manage state cleanly and avoid having multiple conflicting sources of truth. For example, store the canonical event data and derive any grid representation from it as needed.
Assume a mockup is provided during the interview. Focus on building the core interaction and state model.
Quick Answer: This question evaluates frontend UI engineering skills, focusing on interactive interface construction, event handling, and state modeling to maintain a single source of truth for events in a weekly calendar grid, and it belongs to the web frontend development and UI/state-management domain.
You are implementing the state layer behind an interactive weekly calendar UI.
The calendar has 7 days indexed from 0 to 6 and 24 one-hour slots indexed from 0 to 23. Each cell can hold at most one event name.
Process a list of user actions:
- ("click", day, hour): select a cell if the coordinates are valid.
- ("submit", name): if a cell is currently selected, store name in that cell, replacing any existing event there. If name is an empty string, remove any event from that cell instead.
Additional rules:
- If a click is invalid (day not in 0..6 or hour not in 0..23), ignore it and keep the current selection unchanged.
- If submit is called when no cell is selected, ignore it.
- After any submit that uses a selected cell, clear the selection.
Return the final canonical event data as a list of (day, hour, name) tuples sorted by day, then hour. A rendering layer can derive the visible 7x24 grid from this single source of truth.
Constraints
- 0 <= len(actions) <= 100000
- Valid day values are 0 through 6
- Valid hour values are 0 through 23
- Event names are strings and may be empty; an empty name clears the selected cell
- At most 168 cells can contain events at any time
Examples
Input: []
Expected Output: []
Explanation: No actions means no events are stored.
Input: [("click", 0, 9), ("submit", "Standup"), ("click", 2, 14), ("submit", "Review")]
Expected Output: [(0, 9, 'Standup'), (2, 14, 'Review')]
Explanation: Two valid click-submit pairs create two events in different cells.
Hints
- Use a hash map keyed by (day, hour) so each calendar cell has a single canonical event entry.
- Track the currently selected cell separately, and only build the sorted output after all actions are processed.