Exercises from the famous Programming Pearls by Jon Bentley.
- Fast system sort implementation using a bitmap data structure along with an efficient algorithm for
sampling
kunique elements in range[1, n], for which I used the floyd's random sampling algorithm. The problem is to design an efficient algorithm to sort a list of1,000,000distinct positive elements all lesser than10,000,000using lesser than2 MBstorage. The sorting algorithm designed here beats the Java system sort by a factor of4.
- Group anagrams in a list of words. Solved by assigning each word a signature that is its sorted form and grouping all words with the same signature together.
- Rotate a vector by
ipositions in-place. Boils down to changing vectorabtobawhereaandbrepresent a contiguous block of elements. This is elegantly solved by employing a bunch ofreverseoperations on the array. - Rotate matrix in place. Solved by using an in-place transposition algorithm.
- Design a clean and maintable algorithm to process tax amounts for various input incomes. Key was to use an array.
- A turnpike consists of
n - 1streches of road betweenntoll stations. Each strech has an associated cost of travel. Design a data structure that requiresO(n)space but allows the cost of any route to be computed in constant time. Key was the use of prefix arrays.
- Implement a Priority Queue.
- Implemented a sequencial disk access method that adds an additional pointer to every node to enable logarithmic index access time. Time and space complexity is
log(n)when there arenelements in the list. The logarithmic access time is achieved by storing an additionaljumppointer at every node. The functionality is similar to that of finding theithpower of a number inlog(i)time. For example, to find the5thelement, we would visit5 -> 4 -> 2 -> 1. - Implemented an algorithm to produce matches given the rankings of players. Used a BFS like approach.
- Find the longest repeated substring in a string. Example,
LRS(banana) = ana. The problem can be solved using suffix arrays. - Given a new input string, how would you search a suffix array to find the longest match in the given text? Solved using binary search.