Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

BJP3 Exercise 14.18: mirrorHalves

Language/Type: Java Collections Stacks and Queues
Author: Marty Stepp (on 2013/04/01)

Write a method mirrorHalves that accepts a queue of integers as a parameter and replaces each half of that queue with itself plus a mirrored version of itself (the same elements in the opposite order). For example, suppose a queue variable q stores the following elements:

front [10, 50, 19, 54, 30, 67] back

After a call of mirrorHalves(q);, the queue would store the following elements:

front [10, 50, 19, 19, 50, 10, 54, 30, 67, 67, 30, 54] back

If your method is passed an empty queue, the result should be an empty queue. If your method is passed a null queue or one whose size is not even, your method should throw an IllegalArgumentException.

You may use one stack or one queue (but not both) as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original queue. Use the Queue interface and Stack/LinkedList classes from lecture.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.