Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement fundamental collections using resizing arrays and linked lists, and to introduce you to generics and iterators.

Deque. A double-ended queue or deque (pronounced “deck”) is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the collection. Create a generic data type Deque that implements the following API:

public class Deque<Item> implements Iterable<Item> {

    // construct an empty deque
    public Deque()

    // is the deque empty?
    public boolean isEmpty()

    // return the number of items on the deque
    public int size()

    // add the item to the front
    public void addFirst(Item item)

    // add the item to the back
    public void addLast(Item item)

    // remove and return the item from the front
    public Item removeFirst()

    // remove and return the item from the back
    public Item removeLast()

    // return an iterator over items in order from front to back
    public Iterator<Item> iterator()

    // unit testing
    public static void main(String[] args)

}

Corner cases.  Throw the specified exception for the following corner cases:

Unit testing.  Your main() method must call directly every public constructor and method to help verify that they work as prescribed (e.g., by printing results to standard output). Partial test clients are provided in the Checklist, along with suggestions for additional tests.

Performance requirements.  Your implementation must achieve the following worst-case performance requirements:

Randomized queue. A randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random among items in the collection. Create a generic data type RandomizedQueue that implements the following API:

public class RandomizedQueue<Item> implements Iterable<Item> {

    // construct an empty randomized queue
    public RandomizedQueue()

    // is the randomized queue empty?
    public boolean isEmpty()

    // return the number of items on the randomized queue
    public int size()

    // add the item
    public void enqueue(Item item)

    // remove and return a random item
    public Item dequeue()

    // return a random item (but do not remove it)
    public Item sample()

    // return an independent iterator over items in random order
    public Iterator<Item> iterator()

    // unit testing
    public static void main(String[] args)

}

Iterator.  Each iterator must return the items in uniformly random order. The order of two or more iterators to the same randomized queue must be mutually independent; each iterator must maintain its own random order.

Corner cases.  Throw the specified exception for the following corner cases:

Unit testing.  Your main() method must call directly every public constructor and method to help verify that they work as prescribed (e.g., by printing results to standard output). Partial test clients are provided in the Checklist, along with suggestions for additional tests.

Performance requirements.  Your implementation must achieve the following performance requirements:

Client. Write a client program Permutation.java that takes an integer k as a command-line argument; reads a sequence of strings from standard input using StdIn.readString(); and prints exactly k of them, uniformly at random. Print each item from the sequence at most once.

~/Desktop/queues> more distinct.txt
A B C D E F G H I

~/Desktop/queues> java-algs4 Permutation 3 < distinct.txt
C
G
A

~/Desktop/queues> java-algs4 Permutation 3 < distinct.txt
E
F
G
~/Desktop/queues> more duplicates.txt
AA BB BB BB BB BB CC CC

~/Desktop/queues> java-algs4 Permutation 8 < duplicates.txt
BB
AA
BB
CC
BB
BB
CC
BB

Your program must implement the following API:

public class Permutation {
   public static void main(String[] args)
}

Command-line argument.  You may assume that 0 ≤ kn, where n is the number of strings on standard input. Note that you are not given n.

Performance requirements.  Your implementation must achieve the following worst-case performance requirements:

Submission. Submit the programs, Deque.java, RandomizedQueue.java and Permutation.java. Your submission may not call library functions except those in StdIn, StdOut, StdRandom, java.lang, java.util.Iterator, and java.util.NoSuchElementException. In particular, do not use either java.util.LinkedList or java.util.ArrayList. Finally, submit acknowledgments.txt.

Grading.

file points
Deque.java 17
RandomizedQueue.java17
Permutation.java 6
40

Bonus points: You can earn 2 points for conserving memory in Permutation.java (see performance requirements section above). Note that assignment scores are capped at 40.


This assignment was developed by Kevin Wayne.
Copyright © 2026.