Facebook

  www.facebook.com
  www.facebook.com

Interview Question

Software Engineer Interview San Francisco, CA

I had 4 consecutive interviews: first one was a general

  talk about my experience, second was to write a json beautifer, third was to design their newsfeed, and last was to write a program that prints all subsets of size k of a given set with n integers.
Answer

Interview Answer

1 Answer

0

(all subsets of size k)

public static void printAllSubsets(int[] a, int k) {
    printAllSubsets(a, 0, "", k);
}

public static void printAllSubsets(int[] a, int i, String soFar, int k) {
    if (0 == k) {
        System.out.println(soFar);
    } else if (i < a.length) {
        printAllSubsets(a, i+1, soFar, k);
        printAllSubsets(a, i+1, soFar + a[i], k-1);
    }
}

Rahul on May 8, 2013

Add Answers or Comments

To comment on this Question, Sign In with Facebook or Sign Up