Why Algorithms Matter


Exercises for Chapter #2 of A Common-Sense Guide To Data Structures and Algorithms

Get The Book!

Chapter Exercises


Exercise #1

There’s an age old puzzle that goes as follows: You’re at a river with two buckets. One holds exactly 3 liters, and one holds exactly 5 liters. Figure out how to measure out exactly 4 liters using those two buckets.

  1. Develop two different algorithms for measuring out 4 liters.
  2. Which of your algorithms is more efficient? That is, how many steps does each algorithm take?

Exercise #2

How many steps would it take to insert the number 7 into the ordered array of [2, 4, 6, 8, 10, 12]?

Exercise #3

How many steps would it take to search for the number 8 in the ordered array: [2, 4, 6, 8, 10, 12]?

Exercise #4

How many steps does it take to perform binary search on array of size 200?

Exercise #5

How many steps does it take to perform binary search on array of size 400?

Exercise #6

Use your favorite object-oriented programming language to create a class that represents an ordered array. It should include functions/methods that serve as the key operations: Read, search, insert, and delete. Ensure that the insert operation inserts the value in the correct place within the ordered array.

Let's Get In Touch!


Join the forum on The Pragmatic Programmers website to join in on the conversation about this book, or email Jay Wengrow, author of the book and this website directly.