Skip to content
Related Articles

Related Articles

Data Structures | Queue | Question 11
  • Last Updated : 27 Aug, 2013
GeeksforGeeks - Summer Carnival Banner

Consider the following pseudo code. Assume that IntQueue is an integer queue. What does the function fun do?

void fun(int n)
    IntQueue q = new IntQueue();
    for (int i = 0; i < n; i++)
        int a = q.dequeue();
        int b = q.dequeue();
        q.enqueue(a + b);

(A) Prints numbers from 0 to n-1
(B) Prints numbers from n-1 to 0
(C) Prints first n Fibonacci numbers
(D) Prints first n Fibonacci numbers in reverse order.

Answer: (C)

Explanation: The function prints first n Fibonacci Numbers. Note that 0 and 1 are initially there in q. In every iteration of loop sum of the two queue items is enqueued and the front item is dequeued.

Quiz of this Question

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :