FastPrepCustomer Support Line With Cancellations

Customer Support Line With Cancellations

Capital One logoCapital One● EasyNEW GRADONSITE INTERVIEW

Updated

Problem statement

A customer support desk answers customers in the order they got in line. The line starts empty, and a customer has at most one place in it at a time. Run the entries of commands in order. Each one is:

  • "SUBMIT name": if name is not in line, put that customer at the back and return "OK". If the customer is already waiting, return "DUPLICATE" and leave the line as it is.
  • "SERVE": take the customer at the front out of the line and return their name, or return "NONE" if the line is empty.
  • "CANCEL name": take that customer out of the line, wherever they are, and return "OK"; return "NOT_FOUND" if they are not in line.
  • "POSITION name": return the customer's place in line as a decimal string, "1" for the front, or "NONE" if they are not in line.

A customer who has been served or cancelled is no longer in line and may submit again; they then go to the back. Return one string per command, in the order of commands.

Function

runSupportQueue(commands: String[]) → String[]

Examples

Example 1

commands = ["SUBMIT ann","SUBMIT bo","SUBMIT cy","POSITION cy","SERVE","POSITION cy","SUBMIT ann","SERVE","SERVE","SERVE","SERVE"]return = ["OK","OK","OK","3","ann","2","OK","bo","cy","ann","NONE"]

The line is ann, bo, cy, so cy is in place 3. SERVE removes ann, the earliest, and cy moves up to place 2. When ann submits again she goes to the back of the line, behind cy. The next three SERVE commands return bo, cy and ann, and the last finds nobody.

Example 2

commands = ["SUBMIT dee","SUBMIT eli","SUBMIT dee","POSITION dee","CANCEL dee","CANCEL dee","POSITION eli","SUBMIT dee","POSITION dee","SERVE","SERVE","SERVE","CANCEL fay","POSITION fay"]return = ["OK","OK","DUPLICATE","1","OK","NOT_FOUND","1","OK","2","eli","dee","NONE","NOT_FOUND","NONE"]

dee is already waiting, so the second SUBMIT dee is a duplicate and she keeps place 1. After CANCEL dee she is out, so a second cancel answers NOT_FOUND, and eli has moved up to place 1. When dee submits again she joins behind eli, in place 2. The serves return eli, dee and then NONE. fay never joined, so cancelling her gives NOT_FOUND and her place is NONE.

Example 3

commands = ["SUBMIT a1","SUBMIT b2","SUBMIT c3","SUBMIT d4","CANCEL b2","POSITION d4","CANCEL a1","SERVE","POSITION d4","SERVE","SERVE"]return = ["OK","OK","OK","OK","OK","3","OK","c3","1","d4","NONE"]

Cancelling b2 from the middle leaves a1, c3, d4, so d4 is in place 3. Cancelling the front customer a1 leaves c3, d4. SERVE then returns c3, and d4 moves up to place 1. The last two serves return d4 and NONE.

Constraints

  • 1 <= commands.length <= 2000
  • Each command has one of the four forms above, with a single space, and is 5 to 15 characters long.
  • Every customer name is 1 to 6 lowercase letters or digits.

More Capital One Onsite Interview questions

Similar questions at other companies

Capital One interview experiences

All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public String[] runSupportQueue(String[] commands) {
        // Write your code here.
    }
}
Checking account…