0% found this document useful (0 votes)
90 views6 pages

Evaluating Stacks and Queues in Java

The document discusses stacks, queues, and algorithms analysis questions. For question 8, a ticket counter simulation is run with 200 customers per day, a service time of 5 minutes per customer, and customers arriving every 20 seconds. The output shows that with 10 cashiers, the average customer wait time is below 10 minutes.

Uploaded by

moh543senob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views6 pages

Evaluating Stacks and Queues in Java

The document discusses stacks, queues, and algorithms analysis questions. For question 8, a ticket counter simulation is run with 200 customers per day, a service time of 5 minutes per customer, and customers arriving every 20 seconds. The output shows that with 10 cashiers, the average customer wait time is below 10 minutes.

Uploaded by

moh543senob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Stacks:

Q1: Please show how the following postfix expression can be evaluated using Stack as shown in

the class. Please show the stack contents and the operations.

4 7 5 3 + * 16 9 - / *

Ans: 32

Q2: A program performs the following operations on a stack S and Queue Q:

a. [Link](52);

b. [Link](52);

c. [Link]([Link]()*2);

d. [Link]([Link]()); 104 36

e. [Link]([Link]()); 52 28

f. [Link](); 52 0

g. [Link]([Link]());

h. [Link](36); stack Queue

i. [Link](28);

j. [Link]([Link]()%3);

k. [Link]([Link]())

l. [Link]([Link]());

m. [Link]([Link]());

The output from stack and Queue is:

104

36

36
Q3: What are the benefits of implementing stack using a LinkedList rather than an array based

implementation?

ANS: implementing stack implementation array

LinkedList<string> ArrayList<Integer>

LinkedList< Integer> ArrayStack<T>

LinkedList< T> ArrayList<String>

push()

pop() add ()

peek() remove()

isEmpty() set()

size() get()

toString() size()

Queue:

Q4: A program performs the following operations on an empty queue Q:

n. [Link](24)

o. [Link](74)

p. [Link](34)

q. [Link]()

r. [Link]()

s. [Link](12)

t. [Link]()

Please show the queue contents at the end of these operations. Clearly show the front and rear of

the queue.

24 74 34 12

34 12
Algorithm Analysis:

Q5: Please identify the time complexity of following code (using Big O notation). Assume that

eval method has a time complexity of O(n). Briefly explain why?

int val = 2*n;

for(int i = 1; i < val; i=i++)

eval(val);

Ans: O(n2)

Q6. What is the time complexity (in Big O) of the method test_code? Assume that test_1 has

complexity O(logn), test_2 has complexity O(n) and test_3 has complexity O(n 3). Briefly explain

why?

Ans: O(n3)

Q7. A program creates a queue. The program takes array of user names as String and then performs

following operations:

- If the user name starts with A to S, it will add (enqueue) to the queue

- If the user name starts with T to Z, it will remove (dequeue) one element from the queue and
then add both the user names to the queue.

What will the queue look like if the input array is as follows? Please clearly label front and rear of the

queue.

[“Saeed”, “Akram”, “Jamal”, “Tanveer”, “Usman”, “Ahmed”, “Dawood”, “Waheed”]

Ans: [“Tanveer”, “Saeed”, “Usman”, “Akram”, “Ahmed”, “Dawood”, “Waheed”, “Jamal”]

Q8. Please run the ticket counter simulation code with following parameters:

a. Number of customers per day = 200

b. Single Customer Service Time = 5 minutes

c. A customer arrives in the system every 20 seconds

How many cashiers are needed to keep the average customer wait time below 10 minutes?

Ans:

public class TicketCounterExample

private final static int PROCESS = 600;

private final static int MAX_CASHIERS = 200;

private final static int NUM_CUSTOMERS = 250;

public static void main(String[] args)

Customer customer;

Queue<Customer> customerQueue = new LinkedList<Customer>();

int[] cashierTime = new int[MAX_CASHIERS];

int totalTime, averageTime, departs, start;

for (int cashiers = 0; cashiers < MAX_CASHIERS; cashiers++)

{
for (int count = 0; count < cashiers; count++)

cashierTime[count] = 0;

for (int count = 1; count <= NUM_CUSTOMERS; count++)

[Link](new Customer(count * 20));

totalTime = 0;

while (!([Link]()))

for (int count = 0; count <= cashiers; count++)

if (!([Link]()))

customer = [Link]();

if ([Link]() > cashierTime[count])

start = [Link]();

else

start = cashierTime[count];

departs = start + PROCESS;

[Link](departs);

cashierTime[count] = departs;

totalTime += [Link]();

averageTime = totalTime / NUM_CUSTOMERS;

[Link]("Number of customers per day : " + (cashiers + 1));

[Link]("Average time: " + averageTime + "\n");

}
}

Common questions

Powered by AI

To reduce the average customer wait time below 10 minutes in the ticket counter simulation, one must adjust the number of cashiers as per the parameters provided: (a) 200 customers per day, (b) 5 minutes service time, and (c) customer arrival every 20 seconds. By simulating different numbers of cashiers and calculating average wait times, one finds the minimal number of cashiers that ensures the average wait time does not exceed 10 minutes. The required computing workload simulation solves the equation for each cashier count, with the program iteratively calculating and printing these until finding the suitable configuration .

The ticket counter simulation processes an average of 200 customers per day with each customer serviced in 5 minutes and each arriving every 20 seconds. Results show that around 8 to 10 cashiers are needed to maintain an average customer wait time below 10 minutes. Incremental trials with different numbers of cashiers reveal the point at which the balance between service time and arrival rate achieves desired wait time goals, considering the constraints provided in the simulation framework .

The time complexity for the provided code snippet is O(n^2). This is because the code contains a loop running approximately "2*n" times (for "int i = 1; i < val; i=i++"), where each iteration involves calling the "eval(val)" method. Given the "eval(val)" method has a time complexity of O(n), the nested relationship between the loop (O(n)) and the method makes the total complexity O(n) * O(n) = O(n^2) for performing this operation across the entire loop .

Starting with an empty queue and performing operations - n. "Q.enqueue(24)", o. "Q.enqueue(74)", p. "Q.enqueue(34)", q. "Q.first()", r. "Q.dequeue()", s. "Q.enqueue(12)", t. "Q.dequeue()" - results in the queue containing [34, 12] at the end of the operations. The position of its front is "34" and the rear is "12" after these operations, where "24" and "74" are dequeued during the process .

The stack class using LinkedList implements methods such as push, pop, peek, and isEmpty. These operations are efficient due to LinkedList's dynamic nature allowing O(1) time complexity operations. In contrast, array-based stacks, while straightforward, require predefined sizes and potential resizing, which makes operations like push incur O(n) in worst cases due to resizing needs. LinkedLists efficiently manage memory without overhead due to unnecessary space reservations found in arrays, enabling flexibility and consistent operation times .

The stack operations: S.push(52), S.push(52), S.push(S.peek()*2), S.pop(), S.pop(), and S.pop() yield the outputs 104 and 36 for size and pop operations respectively. Subsequently, performing sequence operations, the stack outputs are: System.out.println(S.size()) results in 3, System.out.println(S.pop()) results in 104, another pop results in 52, and then size is 1. Queue operations of enqueue and dequeue yield: Q.enqueue(36), Q.enqueue(28), enqueue(Q.first()%3) lead to a final queue setup with outputs: System.out.println(Q.first()) prints 104, System.out.println(Q.dequeue()) prints 1 .

Implementing a stack using a LinkedList offers several advantages over an array-based implementation. LinkedLists provide dynamic sizing, meaning there's no need to allocate a fixed amount of memory initially; memory is allocated as needed, which is efficient for memory use. LinkedList-based stacks also enable constant time operations (O(1)) for push and pop operations, while array-based implementations can require resizing, which can be costly. Furthermore, LinkedLists do not suffer from the fixed maximum size limitations that array-based stacks do unless constrained by system memory .

To evaluate the postfix expression "4 7 5 3 + * 16 9 - / *" using a stack, follow these steps: 1. Push 4 onto the stack. 2. Push 7 onto the stack. 3. Push 5 onto the stack. 4. Push 3 onto the stack. 5. Pop 3 and 5, add them to get 8, push 8 onto the stack. 6. Pop 8 and 7, multiply to get 56, push 56 onto the stack. 7. Push 16 onto the stack. 8. Push 9 onto the stack. 9. Pop 9 and 16, subtract to get 7, push 7 onto the stack. 10. Pop 7 and 56, divide to get 8, push 8 onto the stack. 11. Finally, pop 4 and 8, multiply them to get 32, which is the result. Throughout these operations, the stack contents change accordingly, showing the intermediate results as elements are computed and pushed back for further computation .

The overall time complexity of the method "test_code" is O(n^3) due to the presence of "test_3" which has a complexity of O(n^3). Even though "test_code" also calls "test_1" (O(log n)) and "test_2" (O(n)), the complexity of the entire method is dominated by the highest complexity among them, which is O(n^3). The most computationally expensive operation dictates the complexity when different operations are composed sequentially .

When processing the queue with the array of names ["Saeed", "Akram", "Jamal", "Tanveer", "Usman", "Ahmed", "Dawood", "Waheed"], the operations are as follows: - "Saeed" and "Akram" are enqueued. - "Jamal" is enqueued. - "Tanveer", starting with T, triggers a dequeue of "Saeed" and then both "Tanveer" and the dequeued "Saeed" are enqueued back. - "Usman" and "Ahmed" are enqueued as they start with allowed letters. - "Dawood" is enqueued. - "Waheed", starting with W, leads to a dequeue of "Akram", and both "Waheed" and "Akram" are enqueued subsequently. This results in the final queue being [“Tanveer”, “Saeed”, “Usman”, “Akram”, “Ahmed”, “Dawood”, “Waheed”, “Jamal”] with "Jamal" being at the rear and "Tanveer" at the front after all operations .

You might also like