Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 6 vote

Typical Dynamic Programming

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

        Set<Integer> sums = new HashSet<>();
        sums.add(0);

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

            if(sums.contains(sum - c1) || sums.contains(sum - c2) || sums.contains(sum - c3)) {
                System.out.println(sum);
                sums.add(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.

- aonecoding4 December 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Alternative approach:

def count_coins(cs, limit=1000):
    ans = [0]
    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:]

- Anonymous December 16, 2018 | Flag Reply
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;
    }
}

- Guy December 16, 2018 | Flag Reply
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]
    sums.add(0)

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

- guilhebl December 16, 2018 | Flag Reply
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];
              }                  
          }
      }      
};

- ashkanxy December 17, 2018 | Flag Reply
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[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;
}

- lesit.jae December 17, 2018 | Flag Reply
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);

- Kathy Khaing December 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 
		}

	}
}

- divm01986 December 18, 2018 | Flag Reply
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)

- Anonymous December 18, 2018 | Flag Reply
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);
}

- Vova January 26, 2019 | Flag Reply
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.

- eileen March 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int change(int amount, int[] coins) {
int[] combinations = new int[amount+1];
combinations[0]=1;

for(int coin : coins){
for(int i=1; i< combinations.length ;i++){
if(i>=coin){
combinations[i]+=combinations[i-coin];
}
}
}

return combinations[amount];
}

- Anonymous April 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- sameer May 29, 2019 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More