Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). This table is sorted along the rows and columns — that is,
Table[i][j] ≤ Table[i][j + 1]
Table[i][j] ≤ Table[i + 1][j]
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). This table is sorted along the rows and columns — that is,
Table[i][j] ≤ Table[i][j + 1]
Table[i][j] ≤ Table[i + 1][j]
Here is an interview question you would expect from a typical Google interview, which is an interesting problem itself. Different solutions from multiple perspectives are provided in this post.
There are n coins in a line. (Assume n is even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.
- Would you rather go first or second? Does it matter?
- Assume that you go first, describe an algorithm to compute the maximum amount of money you can win.
Here order is not important, so don’t print the duplicated combination.
e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3
My Attempt
void Sum(int arr[], int reminder, int index, int visited[]) {
if (index LessThen 0) { return }
if (difference == 0) { printSum(arr, visited) , return; }
if (reminder - arr[index] LessThen 0) { return }
visited[index]++;
Sum(arr, reminder - arr[index] , index, visited) // duplicate number
Sum(arr, reminder - arr[index], index - 1, visited) // next number
visted[index]--;
Sum(arr, reminder, index - 1, visited) // skip
}
Find all possible combination of numbers using a pre-defined candidate set.
Each number from the candidate set may be used only once in the combination.
For example,
Candidate Set = {10, 1, 2, 7, 6, 1, 5}
Target Number = 8
One possible output could be:
1+1+6
1+2+5
1+7
2+6
e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3
// repeat the current index for possible duplicates
}
Here order is not important, so don’t print the duplicated combination.
e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3)
My Attempt
Sum(int arr[], int remainder, int index, int visited[])
{
if (index LessThen 0 ) { return; }
if (difference == 0) {
// bingo!
printAddition(arr, visited); // print all visited numbers
return;
}
if (difference - arr[index] > = 0 ) {
visited[index]++;
Sum(arr[], difference - arr[index], index, visited[]); // repeat the number dont move index
Sum(arr[], difference - arr[index], index - 1, visited[]);
visited[index]--;
}
Modified Problem
Let’s change the question a little bit.
Find all possible combination of numbers using a pre-defined candidate set.
Each number from the candidate set may be used only once in the combination.For example,
Candidate Set = {10, 1, 2, 7, 6, 1, 5}
Target Number = 8One possible output could be:
1+1+6
1+2+5
1+7
2+6