## Facebook Interview Question

Software Engineers**Country:**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
}
}
}
```

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.