Data structures and algorithms

Exercises: Sequences

Note: The exercises are optional, but highly recommended. If you are uncertain about any problem or want to discuss, write in the course discussion channel, or ask a teacher.

The exercises are divided into core and bonus. We recommend you try all the core exercises. Try some of the bonus exercises if you want to learn more.

Core exercises

A1. Give an algorithm that removes all comments from a program. Assume that comments are written as { ... } and can be nested (i.e. { a comment with {another comment} inside }). Write your algorithm in pseudocode if you like. What is the complexity of your algorithm?

Harder: Suppose that there are two different kinds of comments: { ... } and [ ... ]. These must be correctly nested, for example {A [B} C] is not correct, but {A [B] C} is. Design an algorithm that checks if all comments are correctly nested.

A2. Write a program that takes a postfix expression (as a sequence of strings) and evaluates it to an integer. Example: The postfix expression 3 7 5 + 2 / * means 3 * ((7 + 5) / 2) and should evaluate to 18. Which ADT is useful here? (The advantage of postfix expressions is that they don’t need any brackets.)

A3. Recall the following question from the quiz, about queues:

Suppose that a client performs an intermixed sequence of (queue) enqueue and dequeue operations. The enqueue operations put the integers 0 through 9 in order onto the queue; the dequeue operations print out the return value. Which of the following sequence(s) can never occur?

Come up with a rule that tells if a sequence is possible or not.

A4. Design a data structure that represents an array of unlimited size: unlike a normal array, there is no maximum index that can be written to. It should support the following operations:

(This implements a map with natural numbers as keys.)

Hint:

To implement the data structure, use a normal array but automatically grow it when necessary. Use the array-doubling trick to ensure good performance.

Hint:

Note that in set(...), if the index is much bigger than the current size of the array, you may have to double the size more than once.

Bonus exercises

B1. The same as question A3, but for stacks:

Suppose that an intermixed sequence of (stack) push and pop operations are performed. The pushes push the integers 0 through 9 in order; the pops print out the return value. Which of the following sequence(s) can never occur?

Try to come up with a rule that tells if a sequence is possible or not. Note that this is trickier than for queues.

B2. The following exercises are all from Sedgewick & Wayne. Note that some of them (but not all) have solutions or hints on the book website.

Heading “Exercises” on the Sedgewick & Wayne website: https://algs4.cs.princeton.edu/13stacks/

Heading “Web Exercises” on the Sedgewick & Wayne website: https://algs4.cs.princeton.edu/13stacks/

Linked list exercises from Sedgewick & Wayne

Creative problems from Sedgewick & Wayne