Strassen’s method is a good algorithm used in matrix multiplication. Naive approach usually takes O(N^3) to do matrix multiplication, while Strassen’s method costs O(N^2.8). Basically its recursion formula is like T(N) = 7*T(N/2) + N^2.
This is a blog for interview questions. The questions are primarily from certain web forums, BBS or books. The owner of this blog doesn't claim any copyright of those questions. The blog is established primarily to help the blog owner prepare job interviews.
Saturday, February 25, 2012
Friday, February 24, 2012
Get the Maximum Profit from Stock -- Maximum Subarray Problem
Problem: Given an array price[], price[i] denotes the stock price at day i. Assume the array has a length of N (from day 1 to day N), try to get the maximum profit by selecting optimal timing to bug and sell stocks. For example, if price[] = {5, 2, 4, 3, 1}, the maximum profit per share we can get is $2. What we need to do is buy at day 2 and sell at day 3.
Solution: Naive approach takes O(N^2) by checking any two days. An improved approach costs O(NlogN) by first finding a descending sequence from the beginning. However, optimal approach should only cost O(N). The key is to do some transformation on the original problem. Let us still reuse the previous example, given price[] = {5, 2, 4, 3, 1}, we can get another array diff[] = {-3, 2, -1, -2} such that diff[i] = price[i] - price[i-1] . Then other goal is just to find the maximum subarray in diff[]. As known, there is a O(N) solution for the maximum subarray problem.
More: An alternative that may be conceptually even simpler exist. Just do a linear scan, we keep a current_min, then for every day, we calculate the gain as the difference between the price at that day and current_min,if the gain is more than the maximum gain that we are tracking, update the maximum.
This algorithm also takes O(N).
Solution: Naive approach takes O(N^2) by checking any two days. An improved approach costs O(NlogN) by first finding a descending sequence from the beginning. However, optimal approach should only cost O(N). The key is to do some transformation on the original problem. Let us still reuse the previous example, given price[] = {5, 2, 4, 3, 1}, we can get another array diff[] = {-3, 2, -1, -2} such that diff[i] = price[i] - price[i-1] . Then other goal is just to find the maximum subarray in diff[]. As known, there is a O(N) solution for the maximum subarray problem.
More: An alternative that may be conceptually even simpler exist. Just do a linear scan, we keep a current_min, then for every day, we calculate the gain as the difference between the price at that day and current_min,if the gain is more than the maximum gain that we are tracking, update the maximum.
This algorithm also takes O(N).
Friday, February 10, 2012
N Disks and K Pegs -- General Problem of Hanoi Tower.
Problem: Most people must be aware of the Tower of Hanoi problem that has N disks and 3 pegs. Basic Hanoi problem can be solved either by iterative or recursive approach. However, here we discuss a more general problem. Given N disks and K pegs, also given a start state (e.g., the peg that a particular disc is on) and an end state, try to find the optimal solution (i.e., minimum moves) to move the disks such that we can go from the start state to the end state. Here we still need to follow the similar rule in Hanoi: larger disk should be below smaller disk.
For example, assume we have 4 pegs, and the start state is: pegs[1] = [3,2] (which means disk 3 and disk 2 are on peg 1), pegs[2] = 4, pegs[3] = 1, pegs[4] = []; the end state is pegs[1] = [1], pegs[2] = [3], pegs[3] = [], pegs[4] = [4,2]. The optimal solution is 2->4, 1-> 4, 1->2, 3->1 ( here "2->4" means move the top of peg 2 to peg 4), totally 4 moves.
Solution: if the N and K are not too large, we can use DFS to solve this problem. Basically, the total space will be K^N, but we are able to prune out some unfeasible paths.
For example, assume we have 4 pegs, and the start state is: pegs[1] = [3,2] (which means disk 3 and disk 2 are on peg 1), pegs[2] = 4, pegs[3] = 1, pegs[4] = []; the end state is pegs[1] = [1], pegs[2] = [3], pegs[3] = [], pegs[4] = [4,2]. The optimal solution is 2->4, 1-> 4, 1->2, 3->1 ( here "2->4" means move the top of peg 2 to peg 4), totally 4 moves.
Solution: if the N and K are not too large, we can use DFS to solve this problem. Basically, the total space will be K^N, but we are able to prune out some unfeasible paths.
- Basically, we try all the possible moves in a DFS way. If we reach the end state, we stop and return. Meanwhile, we need to remember how many moves it takes to the end and store it in current_min.
- If from the start state, we have already made current_min moves, we don't need to go further, since we can't get a result better than current_min.
- We need to have a hash table to remember the state we have seen before. If we found we revisited a state, try to compare the moves that we have made now and the previous number of moves to reach that state. If now we take less move to revisit the state, we go forward. Otherwise, we can just return.
// the counter for the number of moves from the
// start state
int cnt = 0
// the current minimum moves to the end state
int current_min = INT_MAX
// a hash table to remember the visited states
hash_table ht;
void dfs()
{
if(cnt >= current_min) return;
// get the current state of disks
string stat = get_state()
// if stat is not in hashtable
if(!ht.has_key(stat))
{
ht[stat] = cnt;
}
else
{
// if we take more moves to
// visit this state, we just
// return
if(ht[stat] <= cnt) return;
ht[stat] = cnt;
}
// if we reach the end state
if(stat == end_state)
{
if(current_min > cnt)
current_min = cnt;
return;
}
for(int i=0; i<K; i++)
{
current_disk = peg[i].top();
// iterate all the possible moves
// current_disk can make
for move in possible_move_set
{
make the move;
cnt++;
dfs();
rewind the move;
cnt--;
}
}
}
Wednesday, February 8, 2012
String Permutation without Duplication
Problem: This is a classic problem. Given a string "abc", you need to output all the permutations. They are "abc", "acb", "bac", "bca", "cab" and "cba". While for input that contains duplicate characters, you need to output all unique permutations. For example, input is "abb", then output should be "abb", "bab" and "bba".
Solution: For strings without duplicate characters, the solution is not difficult. To avoid duplication, definitely you can use a hashtable to memorize the permutations that you had outputted. Considering that hashtable may cost a lot of space, some other solutions are desired. However, many solutions on the web actually are incorrect. Here gives a correct solution by using a bit vector (or counting array).
Solution: For strings without duplicate characters, the solution is not difficult. To avoid duplication, definitely you can use a hashtable to memorize the permutations that you had outputted. Considering that hashtable may cost a lot of space, some other solutions are desired. However, many solutions on the web actually are incorrect. Here gives a correct solution by using a bit vector (or counting array).
- The key thing is that if now I am gonna swap a character c to position i, I must guarantee previously there is no c that has been swapped to position i. This is the important constraint to avoid duplicate.
- Then we can use an array to track this. If the string only contains characters that range from 'a' to 'z', then we only need an array of the length 26. The code is as follow:
void permutation(string &s, int idx)
{
if(idx == s.size())
{
cout << s << endl;
return;
}
char a[26] = {0};
for(int i = idx; i < s.size(); i++)
{
if(a[s[i] - 'a'] == 1) continue;
a[s[i] - 'a'] = 1;
swap(s[idx], s[i]);
permutation(s, idx+1);
swap(s[idx], s[i]);
}
}
Monday, February 6, 2012
Efficient Way to Calculate Fibonacci Number
Problem: To give a O(logN) solution to Fibonacci function.
Solution: Solving Fibonacci with recursive function is easy. It is also not difficult to have an iterative solution with O(N) time complexity. Then can we do it even better? A solution based on matrix multiplication can help us achieve that.
Solution: Solving Fibonacci with recursive function is easy. It is also not difficult to have an iterative solution with O(N) time complexity. Then can we do it even better? A solution based on matrix multiplication can help us achieve that.
- Basically we have a 2*2 matrix A = {[1,1], [1,0]} ({[first row], [second row]}). we can see that a[0][0] =1 = fibo(2). Then A*A = {[2,1],[1,1]} and a[0][0] = fibo(3) = 2. Basically we will have A^n = {[fibo(n+1), fibo(n)], [fibo(n), fibo(n-1)]}.
- Then calculating Fibonacci number basically boils down to calculate the power of A. You may think this still takes O(N). However, we can calculate power efficiently (see this link) such that only O(logN) is needed.
Find the Maximums in a Sliding Window
Problem: Given an array of integers and a window that has a size w, slide the window from the left side of the array to the right side, report the maximums in the sliding window during the sliding. For example, a[] = {3,1,5,2,1,4,-3, -2 , 6} and the w = 3. Then at the very beginning, we have [3,1,5] in the sliding window and then the maximum in the window is 5. If we slide the window one step towards the right, we have [1,5,2] in the sliding window and the maximum in the window is still 5. You need to get all these maximums. For this example, the maximum array is {5,5,5,4,4,4,6}.
Solution: It is easy to know naive approaches will take O(wN), since after each sliding operation, we need O(w) time to find the maximum. To improve, you can use skip list. Basically, you have a sorted linked list to represent the integers in the window. Then you build skip list on top of it. For every slide operation, we need to do an insertion and a deletion. Therefore, the complexity will be O(Nlogw). However, a smarter solution from www.leetcode.com can achieve O(N). The details are as follow:
Solution: It is easy to know naive approaches will take O(wN), since after each sliding operation, we need O(w) time to find the maximum. To improve, you can use skip list. Basically, you have a sorted linked list to represent the integers in the window. Then you build skip list on top of it. For every slide operation, we need to do an insertion and a deletion. Therefore, the complexity will be O(Nlogw). However, a smarter solution from www.leetcode.com can achieve O(N). The details are as follow:
- Have a double-ended queue (which you can both manipulate the head and the tail. For initialization, add the indices of the first w integers to the queue. While adding indices, we must guarantee the integers to which those indices in the queue point keep a descending order. Therefore, if the index to be added points to an integer that is bigger than some integers to which some indices already in the queue point, we need to remove all "those" indices first. For the example we showed previously, a[] = {3,1,5,2,1,4,-3, -2 , 6}, after adding the first integer, the state of the queue will be [0], which means a[0] is in the queue; After adding the second integer, the state of the queue will be [0,1], which means a[0], a[1] are in the queue. Since a[0] > a[1], this guarantee a valid descending order. However, when adding the third integer, we need to pop put both "0" and "1", since a[2] is bigger than either a[0] or a[1]. Then after adding the third integer, the state of the queue will be [2], which means only a[2] is in the queue.
- Since it is a sliding window, we also need to retire indices that point to the integers which are out of the window. We can do this by checking the indices in the queue and the current location of the window. If some index is out of bound, we need to remove them.
- The size of the queue will always not be bigger than w. For every integer in a[], it will be inserted in the queue and deleted from the queue for at most once. Therefore, the complexity is O(N).
Thursday, February 2, 2012
Find the Unique Element that Appears k Times in an Array
Problem: Let's first come with some special cases of this problem. Then we can generalize. Given an integer array, only one integer appears 2 times, all the other integers appears 3 times in the array. Find out that integer. For example, for a[] = [2,3,2,4,4,2,3,4], the output should be "3".
Solution: Definitely with hash table or sorting, the problem can be easily solved. What if those approaches are not allowed? Fortunately, we can still use bit operation to solve this problem.
A more general problem: What if we change the problem. Now the problem is given an integer array, only one integer appears k times, all the other integers appears m times in the array (k<m). Find out that integer.
Solution: We can use similar strategy (as shown in above code snippet) to solve the problem.
Solution: Definitely with hash table or sorting, the problem can be easily solved. What if those approaches are not allowed? Fortunately, we can still use bit operation to solve this problem.
- We need some counters to store the bits that just appear once, the bits that appear twice.
- If some bit appear three times, we need to reset those counters.
- Then at the end, the counter that stores the bits that appear twice will be the answer
def foo(list):
# three counters which are used to store
# the bits that appear 1 time, 2 times and
# three times, respectively
ones = 0
twos = 0
threes = 0
for x in list:
# which bits had appeared for 3 times?
# and later we need to reset those
# bits if they also appear in the
# counter "ones" and "twos".
threes = twos & x
twos ^= (ones & x)
twos &= ~threes
ones ^= x
ones &= (~threes & ~twos)
# here we just need to return the counter
# "twos". If we need the unique element
# that appears once (assuming the rest
# all appear 3 times, just return "ones"
# instead.
return twos
A more general problem: What if we change the problem. Now the problem is given an integer array, only one integer appears k times, all the other integers appears m times in the array (k<m). Find out that integer.
Solution: We can use similar strategy (as shown in above code snippet) to solve the problem.
- Basically, we need m counters (or a counter array).
- We have counters[m] = counter[m-1] & x.
- counter[i] ^= counter[i-1] & x and counter[i] &= (~ counters[m] & ~counters[m-1] & ... & ~counters[i+1] )
Subscribe to:
Posts (Atom)