Answer Open-Now and Next-Opening Queries for Restaurant Slots
Last updated: November 29, 2026
Quick Overview
Each restaurant has weekly service slots such as Friday 19:00 to 02:00, where an end time at or before the start means the slot runs past midnight. Build a structure that answers, for a day and time, whether the restaurant is open (and when it closes) or when it next opens.
Swiggy
November 29, 20260
0
0 solved
Each restaurant has weekly service slots such as Friday 19:00 to 02:00, where an end time at or before the start means the slot runs past midnight. Build a structure that answers, for a day and time, whether the restaurant is open (and when it closes) or when it next opens.
Swiggy listings show Opens at 7 PM or Closing soon, and late-night slots that cross midnight (and Sunday into Monday) are a frequent source of bugs. The onsite coding round checks interval normalization, merging, binary search and careful handling of week wraparound.
What the Interviewer Expects
- Converts day and time into minutes of the week
- Splits overnight slots and the Sunday-to-Monday wrap correctly
- Merges overlapping or touching slots
- Uses binary search for each query instead of scanning
- Wraps around to the start of the week for the next opening
- Handles a restaurant with no slots
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you add holiday overrides and temporary closures pushed by the restaurant app?
- Restaurants are in one timezone today. What changes if you support several?
- Search needs open-now filtering over 200,000 restaurants every minute. How do you precompute it?
- How would you represent a restaurant that is open 24x7?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input slots are (day, 'HH:MM', 'HH:MM'). If end <= start the slot ends the next day. Queries are (day, 'HH:MM'); output ('open', closing time) or ('closed', next opening time). Many queries pe...
High-Level Design
Map the week to [0, 10080) minutes with Monday 00:00 as 0. Each slot becomes a half-open interval; overnight slots add 1440 to the end, and anything spilling past the week end is split into `[a, 100...