Facebook Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
void printCoinsUpTo(const vector<int> &coins, int val) {
vector<int> result;
vector<int> idx(coins.size(), 0);
result.push_back(0);
for (int i = 1; i <= val; ++i) {
int minVal = INT_MAX;
for (int i = 0; i < coins.size(); ++i) {
minVal = min(minVal, coins[i] + result[idx[i]]);
}
result.push_back(minVal);
for (int i = 0; i < coins.size(); ++i) {
if (coins[i] + result[idx[i]] == minVal) {
idx[i]++;
}
}
}
for(int i = 1; i < result.size(); ++i) {
cout << result[i] << endl;
}
}
Here is my C++ backtracking solution.
In summary in given vector of coins, we pick one element and deduct the coin value from the given target until we reach 0 or negative number.
class Solution {
private:
int sum;
set<int> h;
public:
void combinationSum(vector<int>& coins, int target) {
sum=0;
dfs (candidates, 0, target);
for (auto t :h)
cout<<t<<" ";
return res;
}
void dfs (vector<int>& cand, int beg, int t) {
if(t<=0) {
return;
}
for(int i =beg ; i<cand.size();++i) {
if (cand[i]<=t) {
sum+= cand[i];
h.insert(sum);
dfs (cand, i, t-cand[i]);
sum-=cand[i];
}
}
}
};
time complexity : n * 'search and insert of set'
= n * (2 * log n)
-> n log n
or
time complexity : log(1) + log(2) + ... + log(n)
= log(n!)
n/2 log(n/2) <= log(n!) <= n log n
so
O(n log n)
void printCoinsSum(const vector<int>& coins, int max_sum) {
int start=coins[0];
for(int i=1;i<coins.size();i++) {
if(start>coins[i])
start=coins[i];
}
set<int> coins_sum;
coins_sum.insert(0);
for(int sum=start;sum<=max_sum;sum++) {
for(int i=0;i<coins.size();i++) {
/* new_sum = coins[i] + previous values
new_sum - coins[i] = previous values
check 'new_sum - coins[i]' is in previous values
*/
if(coins_sum.find(sum-coins[i])!=coins_sum.end()) {
coins_sum.insert(sum);
break;
}
}
}
set<int>::const_iterator it=coins_sum.begin();
++it;
for(;it!=coins_sum.end();it++)
cout<<*it<<endl;
}
With PHP.
There is 3 possibilities to combine three different coins. Number can be valid between 1 to 1000 when get 0 that divided by any of three combination.
function possibleCombinedSums($c1, $c2, $c3){
$combined1 = $c1 + $c2;
$combined2 = $c1 + $c3;
$combined3 = $c2 + $c3;
for($sum = 1;$sum <= 1000;$sum++){
if($sum%$combined1 == 0 || $sum%$combined2 == 0 || $sum%$combined3 == 0){
print $sum.'<br/>';
}
}
}
possibleCombinedSums(5,10,15);
printSums(int[] coins) {
Arrays.sort(coins)
int minCoin = coins[0];
boolean[] sums = new boolean[1000 - coins[0] + 1];
for (int i = 0; i < coins.length; i++) {
fillSums(sums, coins[i], minCoin);
}
for (int i = 0; i < sums.length; i++) {
if (sums[i]) {
system.out.println(i + minCoin);
}
}
}
fillSums(boolean[] arr, int coin, int minCoin) {
arr[coin - minCoin] = true;
for (int i = coin + 1; i - minCoin < arr.length; i++ ) {
if ( (i- coin - minCoin) >= 0 && arr[i - coin - minCoin] ) {
arr[i - minCoin] = true
}
}
}
sums = range(1,1001)
numbers = [10, 15, 55]
new_sums = list(filter((lambda x: x%10 == 0 or x%15 == 0 or x%55 == 0), sums))
print(new_sums)
public coins(int a, int b, int c) {
int step = gcd(a, b);
step = gcd(step, c);
int start = min(min(a, b), c);
for (int i = start; i < 1000; i += step) {
if (canFill(i, 0, a, b, c)) {
System.out.println(i);
}
}
}
private boolean canFill(int needed, int my, int a, int b, int c) {
if (my == needed) {
return true;
} else if (my > needed) {
return false;
} else {
return canFill(needed, my + a, a, b, c)
|| canFill(needed, my + b, a, b, c)
|| canFill(needed, my + c, a, b, c);
}
}
private int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
public class Solution {
private PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
public ArrayList<Integer> intersect(List<Integer> denoms)
{
int sum = 0;
denoms.sort(Comparator.comparingInt(o -> o));
priorityQueue = new PriorityQueue<>();
ArrayList<Integer> result = new ArrayList<>();
while (sum < 1000) {
for (int i = 0; i < denoms.size(); ++i) {
priorityQueue.add(sum + denoms.get(i));
}
sum += priorityQueue.peek();
if (sum < 1000) {
result.add(sum);
}
}
return result;
}
}
def coin_three(a, b, c):
l = [a, b, c]
l.sort()
lout = [0]
index = 1
while True:
next_min = 2000
for i in l:
j = index-1
while True:
if j < 0 or lout[j] + i <= lout[index-1]:
break
if lout[j] + i < next_min:
next_min = lout[j] + i
j = j-1
lout.append(next_min)
print(next_min)
index += 1
if next_min >= 1000:
break
def coin_three(a, b, c):
l = [a, b, c]
l.sort()
lout = [0]
index = 1
while True:
next_min = 2000
for i in l:
j = index-1
while True:
if j < 0 or lout[j] + i <= lout[index-1]:
break
if lout[j] + i < next_min:
next_min = lout[j] + i
j = j-1
lout.append(next_min)
print(next_min)
index += 1
if next_min >= 1000:
break
Keeping it simple with no datastructures or other nonsense
#include <stdio.h>
void coins_helper(a, b, c, na, nb, nc, total) {
if ((a*na + b*nb+ c*nc) == total) {
printf("%dx%d %dx%d %dx%d = %d\n", a, na, b, nb, c, nc, total);
return;
} else if ((a*na + b*nb+ c*nc) > total) {
return;
}
coins_helper(a, b, c, na + 1, nb, nc, total);
coins_helper(a, b, c, na, nb + 1, nc, total);
coins_helper(a, b, c, na, nb, nc + 1, total);
}
void coins(a, b, c, total) {
coins_helper(a, b, c, 0, 0, 0, total);
}
int main(void) {
coins(10, 25, 50, 100);
return 0;
}
./main
10x10 25x0 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x0 50x1 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x0 50x1 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x0 50x1 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x0 50x1 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x0 50x1 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x5 25x2 50x0 = 100
10x0 25x4 50x0 = 100
10x0 25x2 50x1 = 100
10x0 25x2 50x1 = 100
10x5 25x0 50x1 = 100
10x0 25x2 50x1 = 100
10x0 25x0 50x2 = 100
Typical Dynamic Programming
Looking for interview experience sharing and mentors?
- aonecoding4 December 16, 2018Visit A++ Coding Bootcamp at aonecode.com.
Given by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greedy and other advanced algo problems),
and mock interviews.
Our students got offers from G, U, FB, Amzn, Yahoo and other top companies after a few weeks of training.
Welcome to email us aonecoding@gmail.com with any questions. Thanks for reading.