logo Practice-It logo

dequeMystery1

Language/Type: Java Deque Deque mystery collections
Author: Conor Reisman (on 2013/01/29)

What are the contents of the Deque returned by each of the following calls? Write the deque's contents from the client perspective as they would appear by a call to the toString method.

public static Deque mystery(String[] elements) {
    Deque<String> deque = new LinkedList<String>();
    for (int i = 0; i < elements.length; i++) {
        if (i % 3 == 0) {
            deque.addLast(elements[i]);
        } else {
            deque.addFirst(elements[i]);
        }
    }
    return deque;
}

Write the output produced by the method when passed each of the following Deques:

["fee", "fie", "foe", "fum"]
["Dana", "Jake", "Sara", "Mikey", "Janette", "Conor", "April"]
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"]

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.