Chapter 3

From The Algorithm Design Manual Solution Wiki
Jump to navigation Jump to search

Data Structure

Stacks, Queues, and Lists

3.1. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string [math]\displaystyle{ ((())())() }[/math] contains properly nested pairs of parentheses, which the strings [math]\displaystyle{ )()( }[/math] and [math]\displaystyle{ ()) }[/math] do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.

Solution


3.2. Give an algorithm that takes a string [math]\displaystyle{ S }[/math] consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in [math]\displaystyle{ S }[/math], which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from [math]\displaystyle{ S }[/math].)


3.3. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time.

Solution


3.4. Design a stack [math]\displaystyle{ S }[/math] that supports S.push(x), S.pop(), and S.findmin(), which returns the minimum element of [math]\displaystyle{ S }[/math]. All operations should run in constant time.


3.5. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.
(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.
(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.

Solution


3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?


3.7. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible.

Solution

Elementary Data Structures

3.8. Tic-tac-toe is a game played on an [math]\displaystyle{ n * n }[/math] board (typically [math]\displaystyle{ n = 3 }[/math]) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with [math]\displaystyle{ O(n) }[/math] space that accepts a sequence of moves, and reports in constant time whether the last move won the game.


3.9. Write a function which, given a sequence of digits 2–9 and a dictionary of [math]\displaystyle{ n }[/math] words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words.

Solution


3.10. Two strings [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] are anagrams if the letters of [math]\displaystyle{ X }[/math] can be rearranged to form [math]\displaystyle{ Y }[/math] . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] are anagrams.

Trees and Other Dictionary Structures

3.11


3.12


3.13


3.14


3.15


3.16


3.17


3.18


3.19


3.20


3.21

Applications of Tree Structures

3.22


3.23


3.24


3.25


3.26


3.27


3.28


3.29


3.30


3.31

Implementation Projects

3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions.


3.33. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts.

Solution

Interview Problems

3.34. What method would you use to look up a word in a dictionary?


3.35. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval?

Solution


3.36. Write a function to find the middle node of a singly linked list.


3.37. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.

Solution


3.38. Write a program to convert a binary search tree into a linked list.


3.39. Implement an algorithm to reverse a linked list. Now do it without recursion.

Solution


3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time.


3.41. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string.

Solution


3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space.


3.43. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop.

Solution


3.44. You have an unordered array [math]\displaystyle{ X }[/math] of [math]\displaystyle{ n }[/math] integers. Find the array [math]\displaystyle{ M }[/math] containing [math]\displaystyle{ n }[/math] elements where [math]\displaystyle{ M_i }[/math] is the product of all integers in [math]\displaystyle{ X }[/math] except for [math]\displaystyle{ X_i }[/math]. You may not use division. You can use extra memory. (Hint: there are solutions faster than [math]\displaystyle{ O(n^2) }[/math].)


3.45. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space.

Solution


Back to Chapter List