Implement a physician consultation queue that respects state licensing
Company: Oscar
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a telehealth consultation queue system that matches patients with licensed physicians based on jurisdiction.
Patients submit consultation requests tagged with the state they reside in. Physicians are registered with a set of states they are licensed to practice in. A physician may only view and accept consultation requests from patients located in one of their licensed states.
Requests in the queue are identified by a unique `requestId` assigned at submission time. When a physician accepts a request, it must be removed from the global queue so no other physician can accept it. Pending requests visible to a physician must be returned in the order they were submitted (FIFO).
Implement a `TelehealthQueue` class with the following methods:
- `addPhysician(physicianId: str, licensedStates: List[str])`: Register a physician with the set of states they are licensed to practice in. If the physician already exists, replace their licensed states.
- `submitRequest(patientId: str, state: str) -> int`: Submit a consultation request from a patient in the given state. Returns a unique auto-incrementing `requestId` starting at 1.
- `getPendingRequests(physicianId: str) -> List[str]`: Return the list of pending patient ids visible to the given physician (those in their licensed states), in submission order. Returns an empty list if the physician is not registered.
- `acceptRequest(physicianId: str, requestId: int) -> bool`: Accept and remove the request from the queue. Returns true on success, or false if the request does not exist or the physician is not licensed in the patient's state. An unregistered physician is licensed nowhere, so the call returns false.
Example 1:
```text
Input:
["TelehealthQueue", "addPhysician", "submitRequest", "submitRequest", "submitRequest", "getPendingRequests", "acceptRequest", "getPendingRequests"]
[[], ["docA", ["CA", "NY"]], ["pat1", "CA"], ["pat2", "TX"], ["pat3", "NY"], ["docA"], ["docA", 1], ["docA"]]
Output: [null, null, 1, 2, 3, ["pat1", "pat3"], true, ["pat3"]]
Explanation:
q = TelehealthQueue() # empty system
q.addPhysician("docA", ["CA", "NY"]) # docA licensed in CA, NY
q.submitRequest("pat1", "CA") # -> 1
q.submitRequest("pat2", "TX") # -> 2
q.submitRequest("pat3", "NY") # -> 3
q.getPendingRequests("docA") # -> ["pat1", "pat3"] (pat2 in TX hidden)
q.acceptRequest("docA", 1) # -> true (pat1 removed)
q.getPendingRequests("docA") # -> ["pat3"]
```
Example 2:
```text
Input:
["TelehealthQueue", "addPhysician", "addPhysician", "submitRequest", "submitRequest", "acceptRequest", "acceptRequest", "getPendingRequests", "getPendingRequests"]
[[], ["docA", ["CA"]], ["docB", ["TX"]], ["patX", "CA"], ["patY", "TX"], ["docA", 2], ["docB", 2], ["docA"], ["docB"]]
Output: [null, null, null, 1, 2, false, true, ["patX"], []]
Explanation:
q = TelehealthQueue() # empty system
q.addPhysician("docA", ["CA"]) # docA licensed in CA
q.addPhysician("docB", ["TX"]) # docB licensed in TX
q.submitRequest("patX", "CA") # -> 1
q.submitRequest("patY", "TX") # -> 2
q.acceptRequest("docA", 2) # -> false (patY is in TX, not docA's state)
q.acceptRequest("docB", 2) # -> true (patY removed)
q.getPendingRequests("docA") # -> ["patX"]
q.getPendingRequests("docB") # -> []
```
```hint Ordering and removal
Listing needs submission order, yet a physician can accept any request they can see, which is often not the oldest one in the queue. Look for a single structure that keeps insertion order and can also delete an arbitrary entry by key in constant time.
```
```hint Validate before you mutate
`acceptRequest` can fail in more than one way. List every condition that must hold before a request leaves the queue, and make sure a failed call leaves everything unchanged.
```
### Constraints & Assumptions
- `1 <= physicianId.length, patientId.length, state.length <= 20`
- All ids consist of alphanumeric characters.
- `1 <= number of licensed states per physician <= 50`
- At most `10^4` calls total across all methods.
- `requestId` starts at 1 and increments by 1 per submitted request.
- Accepting a non-existent request or one outside the physician's licensed states is a no-op that returns false.
### Clarifying Questions to Ask
- Can one patient have several pending requests at once? If so, should a listing show that patient once per request?
- When `addPhysician` replaces a physician's states, should the change apply right away to requests already in the queue?
- Are state codes normalized before they reach the class (for example, always two upper-case letters), or should the class normalize them?
### What a Strong Answer Covers
- A structure that preserves submission order and removes a request by id without scanning the queue.
- License checks through set membership, applied both when listing and when accepting.
- Every failure path of `acceptRequest` (unknown physician, unknown or already accepted request, state outside the license set) returning false with the queue unchanged.
- The time and space cost of each method, including why listing grows with the total number of pending requests.
- Replacement semantics for re-registration, and how the design changes when listing becomes the bottleneck.
### Follow-up Questions
- Each physician is licensed in a handful of states, but thousands of requests are pending across all states. How would you make `getPendingRequests` faster?
- Two physicians licensed in the same state call `acceptRequest` on the same request at the same moment. How do you make sure exactly one succeeds, first inside one process and then across several servers?
- How would you add urgent requests that go ahead of routine ones while keeping FIFO order within each priority?
Overview: Implement a telehealth consultation queue class where physicians can list and accept only the requests from patients in states they are licensed in, returned in submission order. It tests picking data structures for ordered listing and removal by id, handling every failure path on accept, and stating per-method complexity.