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
k
unique 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,000
distinct positive elements all lesser than10,000,000
using lesser than2 MB
storage. 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
i
positions in-place. Boils down to changing vectorab
toba
wherea
andb
represent a contiguous block of elements. This is elegantly solved by employing a bunch ofreverse
operations 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 - 1
streches of road betweenn
toll 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 aren
elements in the list. The logarithmic access time is achieved by storing an additionaljump
pointer at every node. The functionality is similar to that of finding theith
power of a number inlog(i)
time. For example, to find the5th
element, 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.