Data Structures and Algorithms

Elements A, B and C are added to an empty queue in this order. Then one element is removed, D is added and one is removed, and E is added and one is removed. In what order are the elements removed?

  1. AA, B, C
  2. BC, D, E
  3. CC, B, A
Show answer and explanation

Answer: A. A, B, C

A queue is a first-in, first-out (FIFO) structure: items are removed in the order they were added. After A, B and C are added, the first removal takes A. Even though D and E are added at the back afterward, B and C at the front come out first, so the removal order is A, B, C, and D and E remain. C, D, E is the order you would get from a stack (LIFO), which removes the most recently added item each time. Queues are used for print jobs, for message queues that pass data between tasks, and for breadth-first search.

  • A. Correct. B and C, added earlier, come out before D and E.
  • B. This is the order for a stack (LIFO), which removes the most recently added item each time.
  • C. This is the reverse of the insertion order, not queue order.
Practice this topic Back to topic

Related calculators

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

More Data Structures and Algorithms questions

  1. The expression “3 4 + 2 * 5 −” in reverse Polish notation (postfix notation) is evaluated by pushing each number onto a stack and, for each operator, popping two values, computing the result and pushing it back. What is the result? The left operand of an operator is the value that was pushed earlier.
  2. Which is the correct description of a collision in a hash table?
  3. Keys 10, 17, 24 and 3 are stored in this order in a hash table of size 7 (positions 0 to 6) with the hash function h(k) = k mod 7. Collisions are resolved by linear probing, which moves forward one position at a time (wrapping from 6 to 0) to find a free slot. At which position is key 3 stored?
  4. Which correctly compares chaining (linking the elements at the same position into a linked list) and open addressing (finding another free position within the table) as ways to resolve hash table collisions? The load factor is the number of stored elements divided by the table size.
  5. Which is a prerequisite for using binary search, which compares the target with the middle element and halves the search range each time?