Write a function “runCustomerSimulation” that takes following two inputs
a) An integer ‘n’: total number of computers in a cafe and a string:
b) A sequence of uppercase letters ‘seq’: Letters in the sequence occur in pairs. The first occurrence indicates the arrival of a customer; the second indicates the departure of that same customer.
A customer will be serviced if there is an unoccupied computer. No letter will occur more than two times.
Customers who leave without using a computer always depart before customers who are currently using the computers. There are at most 20 computers per cafe.
For each set of input the function should output a number telling how many customers, if any walked away without using a computer. Return 0 if all the customers were able to use a computer.
runCustomerSimulation (2, “ABBAJJKZKZ”) should return 0
runCustomerSimulation (3, “GACCBDDBAGEE”) should return 1 as ‘D’ was not able to get any computer
runCustomerSimulation (3, “GACCBGDDBAEE”) should return 0
runCustomerSimulation (1, “ABCBCA”) should return 2 as ‘B’ and ‘C’ were not able to get any computer.
runCustomerSimulation(1, “ABCBCADEED”) should return 3 as ‘B’, ‘C’ and ‘E’ were not able to get any computer.
Source: Fiberlink (maas360) Interview
We strongly recommend to minimize your browser and try this yourself first.
Below are simple steps to find number of customers who could not get any computer.
1) Initialize result as 0.
2) Traverse the given sequence. While traversing, keep track of occupied computers (this can be done by keeping track of characters which have appeared only once and a computer was available when they appeared). At any point, if count of occupied computers is equal to ‘n’, and there is a new customer, increment result by 1.
The important thing is to keep track of existing customers in cafe in a way that can indicate whether the customer has got a computer or not. Note that in sequence “ABCBCADEED”, customer ‘B’ did not get a seat, but still in cafe as a new customer ‘C’ is next in sequence.
Below are implementations of above idea.
0 1 0 2 3
Time complexity of above solution is O(n) and extra space required is O(CHAR_MAX) where CHAR_MAX is total number of possible characters in given sequence.
This article is contributed by Lokesh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Check Whether a number is Duck Number or not
- Check if a number ends with another number or not
- Check if a number starts with another number or not
- Find the player who rearranges the characters to get a palindrome string first
- Concatenate strings in any order to get Maximum Number of "AB"
- Find if string is K-Palindrome or not using all characters exactly once
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Check if a large number is divisible by 11 or not
- Number of substrings divisible by 8 but not by 3
- Program to check if a number belongs to a particular base or not
- Check if a number is in given base or not
- Smallest odd digits number not less than N
- How to check Aadhar number is valid or not using Regular Expression
- Check if a string represents a hexadecimal number or not
- Check if a large number is divisible by 4 or not
- Check whether the binary equivalent of a number ends with given string or not
- Get the first letter of each word in a string using regex in Java
- Arrange a binary string to get maximum value within a range of indices
- Get K-th letter of the decoded string formed by repeating substrings