Assorted algorithm questions Flashcards
What is the complexity of building a suffix tree?
By using Ukkonen’s algorithm it is O(n). It is linear.
What computing resources do threads share and not?
Share
- memory
- file handles
Don’t share
- stack
What are some of operating system level synchronization objects?
- monitors (mutual exclusive locks)
- semaphore (special case of a semaphore is a mutex)
- counting semaphores
What is busy waiting? What is the problem with it? In what cases is busy waiting acceptable?
busy waiting: while(!somethingHappened()){}
Problem: consumes cpu cycles that could be better spent doing real work
When is it acceptable:
- when we know that waits are going to be small, and busy wait would be faster than trying to acquire a lock
- in a HPC (high performance computing) environment when there are a lots of CPU cores, each dedicated to a specific task, that would not do other tasks while waiting for a lock. In this case a system may perform better with spinlocks.
Which are examples of O(n) runtime substring search algorithms?
Knuth-Morris-Pratt (KMP)
Boyer-Moore (uses 2 fallback tables instead of one as opposed to KMP)
Rabin-Karp (uses a rolling hash function)
What is external sort and how does it work?
A way of sorting big files that are so large that they don’t fit into RAM.
Method:
- divide the files into chunks that fit in available RAM
- sort each chunk in RAM using any O(n log n) algoritm
- write each sorted chunk into disk
- do an n-way merge of the files on disk or merge them one by one
Describe selection sort and its properties.
Algorithm:
Split the original array into two parts: first part is sorted, the second is not. Search for the smallest element in the unsorted list. Swap it with the first element in the unsorted list. The first element of the unsorted list becomes the last of the sorted.
Best, average and worst case performance is O(n^2).
Space complexity: O(n) total, O(1) auxillary.
Selection sort is not stable! (Uses swaps to move the next small number, which may change the order of items that are larger, if the next smallest is swapped with a large number of higher at higher index than the same at a lower)
Describe insertion sort and its properties.
Algorithm:
Split the array into 2 parts: sorted and unsorted. Take the first element of the unsorted list. Swap it with the previous until it is not smaller than the element before it.
Worst case: O(n^2) (when it is sorted in descending order)
Best case: O(n) (it is already sorted)
Average case: O(n^2)
Space: O(n) + O(1) auxilarry
On what kind of data does bucket sort work the best?
On uniformly distributed data. It ensures that in each bucket you end up with 1 element on average.
Describe bucket sort.
Create a number of buckets (preferrably n number of buckets). Insert each item into one of the buckets based on the value (n*a[i] if a[i] is a number in range 0..1).
Sort each bucket using insertion sort. Iterate over the buckets in order and reinsert the elements into the original bucket.
Worst case: O(n^2). Best case: O(n). Average case: O(n+k) (k is the number of buckets).
Describe Quicksort.
Algorithm: partition the array using a choosen pivot. Values less than the pivot are going into the first half, values greater than or equal to the pivot are on the second half. Repeat the process recursively on the two partitions.
Best case, average case: O(n log n), Worst case: O(n^2).
Space complexity worst case: O(log n)
Is quicksort stable?
Depends on implementation. Usually it is not stable. There are stable versions with additional space complexity (O(n)).
What are the benefits of quicksort over mergesort?
It can be parallelized, utilizing multiple CPUs, cores, threads.
In which cases is quicksort performs worst (O(n^2))?
When the pivot function partitions the array into a n-1 and a 1 size array for every iteration. So it depends on the partition function also, not only in the input. For example it is not enough for the input to be sorted or reverse sorted.
Describe merge-sort and its properties.
Algorithm: Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Worst, best, average: O(n log n)
Space complexity: O(n)
Stable sort.