## Facebook Interview Question for Software Engineers

• 4

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
3
of 5 vote

Typical Dynamic Programming

``````public void printSums(int c1, int c2, int c3) {

Set<Integer> sums = new HashSet<>();

for(int sum = 1; sum <= 1000; sum++) {

if(sums.contains(sum - c1) || sums.contains(sum - c2) || sums.contains(sum - c3)) {
System.out.println(sum);
}
}
}``````

Looking for interview experience sharing and mentors?
Visit 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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Alternative approach:

``````def count_coins(cs, limit=1000):
ans = 
for c in cs:
if c > limit:
continue
for x in ans:
if x+c > limit:
break
ans += [x+c]
return sorted(set(ans))[1:]``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

In Scala:

``````def printCoinCombinations(sum: Int, c1: Int, c2: Int, c3: Int) = {
val sums = mutable.HashSet.empty[Int]

for (i <- 1 to sum) {
if (sums.contains(i - c1) || sums.contains(i - c2) || sums.contains(i - c3)) {
println(i)
}
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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];
}
}
}
};``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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);``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````printSums(int[] coins) {
Arrays.sort(coins)
int minCoin = coins;

boolean[] sums = new boolean[1000 - coins + 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
}

}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

A PreSum problem using HashSet or HashMap.
Why: nums adding up, similar to dp to store the previous value.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.