Google Interview Question for Software Developers


Country: United States




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

//I'm on a phone, so please pardon any typos.
//The solution is sublinear: O(n^(2/3))
//The solution assumes that "1^3+1^3=2" is valid, but that could be trivially changed if clarification was made about the problem statement.
void printGoodNumbers (int n) {
  HashMap<Integer, Integer> sumCount = new HashMap<Integer, Integer>();
  for (int x = 1; x*x*x <= n; x++) {
    for (int y = x; (x*x*x)+(y*y*y) <= n; y++) {
      int sum = (x*x*x)+(y*y*y);
      if(!sumCount.containsKey(sum)) sumCount.put(sum, 0);
      sumCount.put(sum, sumCount.get(sum)+1);
    }
  }
  for(Entry<Integer, Integer> entry : sumCount.entries()) if (entry.value() >= 2) System.out.println(entry.key());
}

- Roger June 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct, but very memory consuming solution: for big enough n (say, long.MaxValue) even for the first 10 iterations by x it allocates 8-digit number of elements in HashMap (for all two cubes sums). It's not gonna work

- Alex M. April 26, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

long long approx_cubth_root( long long n) {
  long long min = 1, max = n;
  long long mid;
  while( min < max ) {
    mid = min + ( max - min) / 2;
    if( mid * mid * mid  == n) return mid;
    else if( mid * mid * mid < n ) min = mid + 1;
    else max = mid - 1;
  }
  if(min * min * min < n ) return min;
  else return max;
}

void print_good_numbers(long long n) {
  long long cubth = approx_cubth_root(n);

  unordered_set<long long> level1;
  set<long long> level2;

  for(long long i = 1; i <= cubth + 1; i++) {
      for( long long j = i; j <= cubth + 1; j++ ) {
        long long cur = i * i * i + j * j * j;
        if( cur <= n ) {
          if( level1.find( cur) == level1.end()){
            level1.insert( cur );
          } else{
            level1.erase(cur);
            level2.insert(cur);
          }
        }
      }
    }
  for(auto itr = level2.begin(); itr != level2.end(); itr++) {
    cout << *itr << " ";
  }
  cout << "\n";
}

- mrsurajpoudel.wordpress.com June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class GoodNumber {
	public GoodNumber() {}
	
	public List<Integer> getAllCubes(int n) {
		List<Integer> allCubes = new ArrayList<Integer>();
		int nextNumber = 1;
		int nextCube = 1;
		while (nextCube <= n) {
			allCubes.add(nextCube);
			nextNumber++;
			nextCube = nextNumber * nextNumber;
		}
		return allCubes;
	}

	public boolean isAGoodNumber(int n) {
		List<Integer> allCubes = getAllCubes(n);
		int distinctWays = 0;
		for(Integer i: allCubes) {
			for(Integer j: allCubes){
				if ((i+j) == n) {
					distinctWays++;
				}
				if (distinctWays >= 4) return true;
			}
		}
		return false;
	}

	public List<Integer> getAllGoodNumbers(int n){
		List<Integer> allGoodNumbers = new ArrayList<Integer>();
		for (int i=1; i<=n; i++){
			if(isAGoodNumber(i)) allGoodNumbers.add(i);
		}
		return allGoodNumbers;
	}

	public static void main(String a[]) {
		GoodNumber gn = new GoodNumber();
		List<Integer> inputArr = gn.getAllGoodNumbers(100);
		for(int i:inputArr){
			System.out.print(i);
			System.out.print(" ");
		}
	}
}

- brobada June 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void getGoodNumbers(int limit) {
List<String> output = new ArrayList<>();
int i = 1;
int jMax = (int) Math.cbrt(limit);
while (i < jMax - 3) {
int j = i + 3;
while (j <= jMax) {
long iCube = i * i * i;
long jCube = j * j * j;
long n = iCube + jCube;
if (n > limit) {
break;
}
// System.out.println("****i,j******");
// System.out.println(i + "\t" + j);
int start = i + 1;
int end = j - 1;
while (start < end) {
// System.out.println("****start,end******");
// System.out.println(start + "\t" + end);
long res = (start * start * start) + (end * end * end);
if (res == n) {
output.add(start + " & " + end + " AND " + i + " & " + j);
break;
} else if (res < n) {
start++;
} else {
end--;
}
}
j++;
}
i++;
}
System.out.println(output);
}

- darklight July 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

are negative numbers allowed (to form sum 't' ). With just positive numbers it seems mathematically impossible. Can you give counter example

- anonymous July 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dynamic answer:

countArray[1 to n] = 0
sumsArray[0 to n] = 0..n

loop ii = 2 to cube root of n
    cube = ii^2
    loop jj = cube to n
        numberSums = 1 + sumsArray[jj-cube+1]
        if numberSums == 2
            countArray[jj]++
        end
        if numberSums < table[jj]
            table[jj] = numberSums
        end
    endLoop
endLoop

for ii = 1 to n
    if countArray[ii] == 2
        display ii
    end
end loop

- Anonymous July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Solution {


    public static void main(String [] args) {

        printGoodNumbers(1000000);

    }


    public static boolean isGoodNumber(int number, List<Integer> cubes) {

        int ways = 0;

        for (int i = 0; i < cubes.size(); i++) {
            for (int j = i+1; j < cubes.size(); j++) {
                if (cubes.get(i) + cubes.get(j) == number) {
                    ways++;
                    if (ways == 2) {
                        return true;
                    }
                }
                if (cubes.get(i) + cubes.get(j) > number) {
                    break;
                }
            }
        }

        return false;
    }

    public static void printGoodNumbers(int limit) {

        List<Integer> cubes = new ArrayList<>();

        int i = 1;
        int cube = 1;
        while (cube <= limit) {
            cubes.add(cube);
            i++;
            cube = i * i * i;
        }

        for (int l = 1; l < limit; l++) {
            if (isGoodNumber(l, cubes)) {
                System.out.println(l);
            }
        }

    }

}

- ugurdonmez87 July 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fun fact: These numbers are better known as "Taxicab Numbers" and the first one is 1729.

- kefeilei87 September 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from itertools import combinations
from collections import defaultdict
def find_good_number(n):
    upper_bound = int(n**(1./3.))
    cubes = [i**3 for i in xrange(upper_bound + 1)]
    sums = defaultdict(list)
    for i in combinations(cubes, 2):
        sums[sum(i)].append(i)
    
    result = [k for k,v in sums.items() if len(v) > 1 and k <= n]
    return result

print find_good_number(5000)

- Nitish Garg January 13, 2017 | 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