Question

In: Computer Science

What is the output of the StackQueueMystery algorithm (below) when we have enqueued 1, 2, 5,...

What is the output of the StackQueueMystery algorithm (below) when we have enqueued 1, 2, 5, 6, 3, 7, 4, 10, 8, 9 on q and pushed 8, 6, 9, 7, 10, 5, 4, 3, 2, 1 on stk (in that order) before calling this algorithm. Show your work.

StackQueueMystery:

Input: stk: stack with integers 1-n

Input: q: queue with integers 1-n

Input: n: size and range of stk and q

Pseudocode:

count = 0

while stk and q are not empty:

{

    x = x0 = stk.pop()

    y = y0 = q.dequeue()

    while x != y0 and stk is not empty

        x = stk.pop()

    while y != x0 and q is not empty

        y = q.dequeue()

    count = count + 1

}

return count

Solutions

Expert Solution

Solution:-

The output of the StackQueueMystery algorithm will be 5 as the value of the count will be 5 at the end.

In this question we are given with a stack and a queue. The sequence of push and enqueue that is given can be shown as

as per the pseudo code, the value of count initially is 0 and changes as the while loop is executed

for 1st iteration

  • x = x0 = stk.pop // x = x0 = 1
  • y = y0 = q.dequee() // 1
  • both inner while loop = false.
  • count = count + 1 // 1

for 2nd iteration

  • x = x0 = stk.pop // x = x0 = 2
  • y = y0 = q.dequee() // 2
  • both inner while loop = false.
  • count = count + 1 // 2

For the 3rd iteration

  • x = x0 = stk.pop // x = x0 = 3
  • y = y0 = q.dequee() // 5

while x! = y0 and stk is not empty // true .
it pops elements until its found 5 or stk becomes empty.
5 was found at 5th pos.

while y!=x= and q is not empty // true.
it continues until q becomes empty or its finds 3 .
3 was found at 5th pos.

  • count = count + 1 // 3

for the 4th iteration

  • x = x0 = stk.pop // x = x0 = 10
  • y = y0 = q.dequee() // 3

while x! = y0 and stk is not empty // true .
it pops elements until its found 7 or stk becomes empty.
7 was found at 7th pos.

while y!=x= and q is not empty // true.
it continues until q becomes empty or its finds 10 .
10 was found at 8th pos.

  • count = count + 1 // 4

for the 5th iteration

  • x = x0 = stk.pop // x = x0 = 8
  • y = y0 = q.dequee() // 9

while x! = y0 and stk is not empty // true .
it pops elements until its found 8 or stk becomes empty.
8 was found at 10th pos.

while y!=x= and q is not empty // true.
it continues until q becomes empty or its finds 9 .
9 was found at 10th pos.

  • count = count + 1 // 5 ( result)

here both the stk and q got empted and the value of count becomes 5.

if u like my answer then please give a thumbs up..!!


Related Solutions

Explain briefly what the following algorithm does and write the output below: (5 credits) Step 1:...
Explain briefly what the following algorithm does and write the output below: (5 credits) Step 1: Set sum equal to 0, set n equal to 1 Step 2: Repeat steps 3, 4 and 5 until sum > 60 Step 3: Set sum equal to sum + n2 Step 4: Replace n with n +1 Step 5: Print n and sum on a new line Step 6: Stop Show output:
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
1. When do we say an algorithm is optimal for a problem? Explain with a specific...
1. When do we say an algorithm is optimal for a problem? Explain with a specific problem and two concrete examples of algorithms, one of which is optimal and one which is not optimal for your chosen problem.
Python. 5) What will the code below do? (Assume that we have a dataset df with...
Python. 5) What will the code below do? (Assume that we have a dataset df with these two columns named Occupation' and 'Age') df.groupby('Occupation')['Age'].mean() a) It will return the average age per occupation b) It will return an error c) It will return the total age per occupation d) None of the options 6) df.describe() will return basic descriptive statistics only for numerical variables True/False ? 7) Pandas dataframes can be converted into numpy arrays Truse/False ?
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6};...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6}; and we also have a linked list L of 5 entries 1 -> 2 -> 3 -> 5 -> 6, where 1 is the first in the linked list L, followed by 2 etc. We want to put a new item 4 between 3 and 5 in the array a and in the linked list L (a) Explain in plain English how you do...
Determine the output of the algorithm below the number of assignment operations in each (show work)...
Determine the output of the algorithm below the number of assignment operations in each (show work) the number of print operations in each (show work) the complexity of each algorithm in terms of Big O notation (show work) 3. Let n be a given positive integer, and let myList be a three-dimensional array with capacity n for each dimension. for each index i from 1 to n do { for each index j from 1 to n/4 do { for...
Determine a) the output of each algorithm below b) the number of assignment operations in each...
Determine a) the output of each algorithm below b) the number of assignment operations in each (show work) c) the number of print operations in each (show work) d) the complexity of each algorithm in terms of Big O notation (show work) 1. Let n be a given positive integer, and let myList be a three-dimensional array with capacity n for each dimension. for each index i from 1 to n do { for each index j from 1 to...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
1. What is A-Star (A*) Algorithm in Artificial Intelligence? 2. A* Algorithm Steps 3. Why is...
1. What is A-Star (A*) Algorithm in Artificial Intelligence? 2. A* Algorithm Steps 3. Why is A* Search Algorithm Preferred? 4. A* and Its Basic Concepts 5. What is a Heuristic Function? 6. Admissibility of the Heuristic Function 7. Consistency of the Heuristic Function 8. Find an Implementation in Java, C or Python just choose in which programming language you prefer only select one.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT