Practice: Lists, Stacks, and Queues


💡 This is your chance to put what you've learned into action.

Try solving these practice challenges to check that you understand the concepts. No submission is necessary for practice exercises.

Why practice?

Practice coding helps you become a great coder. These practice problems aren't graded, but that doesn't mean they aren't important.

You should aim to practice a lot, especially with unfamiliar concepts. Spread practice over multiple days to take advantage of the spacing effect, which helps you retain new knowledge.

More about practice

Practice helps you understand what you know, and what you don't know. It can be easy to trick yourself into thinking you understand something when you do not -- or that you don't understand when you do. Practicing by writing code or debugging code will help you find out what you really understand, and where you are still confused.

Practice helps build confidence in your coding. The more programs you write, and the more problems you solve, the more you learn that you are a capable coder and problem-solver.

Practice doesn't always feel good - sometimes you'll be stumped! But, practice shouldn't feel super frustrating either. If you find yourself getting angry at yourself or the code, it's a good time to take a break and ask for help.

The solutions to each challenge are available, and you can view a video of the solution below each challenge.

  • Try to go through the whole challenge without using the solution.
  • If you can’t do the challenge without looking the solution, it means you don’t understand the material well enough yet.
  • Try the next practice challenges without looking at the solution. If you need more practice challenges, reach out on Discord.

1. Stack and queue puzzles

  1. Suppose that an intermixed sequence of push and pop operations are performed on an initially empty LIFO stack. The pushes push the integers 0 through 9 in order, and the pops print out the return value. Which of the following sequences could not occur?

    a. 4 3 2 1 0 9 8 7 6 5
    b. 4 6 8 7 5 3 2 9 0 1
    c. 2 5 6 7 4 8 9 3 1 0
    d. 4 3 2 1 0 5 6 7 8 9

  2. A letter means push and an asterisk means pop in the following sequence. Give the sequence of values returned by the pop operations when this sequence of operations is performed on an initially empty LIFO stack.

    G * O C L * * I M * B K * * * I B * * O * *
    
  3. A letter means enqueue and an asterisk means dequeue in the following sequence. Give the sequence of values returned by the dequeue operations when this sequence of operations is performed on an initially empty FIFO queue.

    G * O C L * * I M * B K * * * I B * * O * *
    
Solution video.

2. Implementing the ArrayQueue class

▶️ Access the practice exercise on GitHub Classroom

Watch the video below to see the full solution.

Make sure you give yourself enough time to solve the practice without watching the video. It is really important for your learning.

Solution Video

3. Implementing a queue using two stacks

▶️ Access the practice exercise on GitHub Classroom

Watch the video below to see the full solution.

Make sure you give yourself enough time to solve the practice without watching the video. It is really important for your learning.

Solution Video

4. Using iterators

▶️ Access the practice exercise on GitHub Classroom

Watch the video below to see the full solution.

Make sure you give yourself enough time to solve the practice without watching the video. It is really important for your learning.

Solution Video