Google Interview Question for Software Engineers


Country: United States




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

I am just writing my thoughts on this:
use hash map and get all the numbers.

for (i=0;i<10;i++)
		for (j=0;j<10;j++)
			hashmap[x,a,b] = a^3+b^3;
	for (i=0;i<10;i++)
		for (j=0;j<10;j++)
			if c^3+d^3 == hashpmap[x]
				printf("a, b, c, d")

- aka August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Too much memory consumption.

- shabgard August 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since the nos (a,b,c,d) are always positive.
Is there a case when a is not equal to c or d?
What I mean is that, is it safe to assume that the cube summation will only be equal to itself i.e. a^3+b^3 = c^3+d^3 only when ((a = c && b=d) or (a =d&&b=c))

- Anon August 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do we know if there is a solution where a is not equal to c or d when the equation is satisfied for positive integers?

- Anon August 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

jh

- Anony August 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are many solutions for this. Search for Taxicab Numbers.

- Anonymous September 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for (i=0;i<10;i++)
		for (j=0;j<10;j++)
			hashmap[x, a, b] = a^3+b^3;
for (i=0;i<10;i++)
		for (j=0;j<10;j++)
			if (hashmap[x] == c^3+d^3)
				printf("a b c d");

- aka August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a hashmap:

//MehrdadAP

void solve(int N)
{

	unordered_map<long long int,vector< pair<int,int> > > umap;

	for (int i=0;i<=N;i++)
		for (int j=i;j<=N;j++){
			long long int tmp = i*1LL*i*aLL*i+j*1LL*j*1LL*j;
			umap[tmp].push_back({i,j});
		}

	for (auto it:umap){
		if (it.second.size()==1) continue;
		for (auto x:it.second)
			cout << "(" << x.first << "," << x.second << ")" << " " ;
		cout << endl;
	}
}

- MehrdadAP August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your output is of the form: (a,b)
It needs to be (a,b,c,d)?

- Anonymous August 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your output is of the form: (a,b)
It needs to be (a,b,c,d)?

- Anonymous August 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

All pairs in a line form an answer (a,b,c,d).

If you assert to have answers in form of (a,b,c,d), you can add two-nested loop for printing each pair of a line in form of (a,b,c,d)

- MehrdadAP August 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a hash table to keep the sums of all possible pairs (i,j).

def a3b3c3d3(n):
  l=[pow(i,3) for i in range(n)]
  d={}
  for i in range(n):
    for j in range(i, n):
      s=l[i]+l[j]
      d.setdefault(l[i]+l[j], []).append((i,j))

  for l in d.values():
    if len(l)>=2:
      first=True
      for i,j in l:
        if (first):
          first=False
        else:
          sys.stdout.write("=")
        sys.stdout.write("{0}^3+{1}^3".format(i,j))
      sys.stdout.write("\n")

- gen-y-s August 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
	{
		Map<Integer,Set<String>> list=new HashMap<Integer,Set<String>>();
		int n=(int) Math.pow(10,2);
		int timecomplexity=0;
		for(int a=0;a<n;a++){
			for(int b=0;b<n;b++){
				Set<String> l;
				timecomplexity++;
				int result=(int) (Math.pow(a, 3)+Math.pow(b, 3));
				if(list.containsKey(result)){
					l=list.get(result);
					l.add(a+","+b);
					list.put(result, l);
				}
				else{
					l=new TreeSet<String>();
					l.add(a+","+b);
					list.put(result, l);
				}
			}
		}
for(Map.Entry<Integer,Set<String>> entry:list.entrySet()){
			Set<String> l=entry.getValue();
			System.out.print(entry.getKey()+": ");
			for(String s:l){
				System.out.print(s+" ; ");
			}
			System.out.println();
		}

- kamal August 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
	{
		Map<Integer,Set<String>> list=new HashMap<Integer,Set<String>>();
		int n=(int) Math.pow(10,2);
		for(int a=0;a<n;a++){
			for(int b=0;b<n;b++){
				Set<String> l;
				int result=(int) (Math.pow(a, 3)+Math.pow(b, 3));
				if(list.containsKey(result)){
					l=list.get(result);
					l.add(a+","+b);
					list.put(result, l);
				}
				else{
					l=new TreeSet<String>();
					l.add(a+","+b);
					list.put(result, l);
				}
			}
		}for(Map.Entry<Integer,Set<String>> entry:list.entrySet()){
			Set<String> l=entry.getValue();
			System.out.print(entry.getKey()+": ");
			for(String s:l){
				System.out.print(s+" ; ");
			}
			System.out.println();
		}
}

- kamal August 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- kamal August 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time Complexity = O(n^2)
The idea is to maintain a hash map of integer and pair-of-numbers as shown below.

public class Main {
	
	public static class Pair{
		int a,b;
		Pair(int a, int b){
			this.a = a;
			this.b = b;
		}
		public String toString(){
			return "("+this.a+", "+this.b+")";
		}
	}
	public static void main(String[] args) {
		int n = 100000;
		printTaxiCabNumbers(n);
	}

	private static void printTaxiCabNumbers(int n) {
		HashMap<Integer, Pair> map = new HashMap<Integer, Pair>();
		for(int i=0; i<n; i++){
			for(int j=i+1; j<n; j++){
				int m = (int) (Math.pow(i, 3)+Math.pow(j, 3));
				if(map.containsKey(m)){
					System.out.println(new Pair(i,j).toString()+" and "+map.get(m).toString());
				}else{
					map.put((int) (m), new Pair(i,j));
				}
			}
		}
	}
}

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

Time Complexity = O(n)
The idea is to maintain a HashMap of sum and Integer-pairs.

public class Main {
	
	public static class Pair{
		int a,b;
		Pair(int a, int b){
			this.a = a;
			this.b = b;
		}
		public String toString(){
			return "("+this.a+", "+this.b+")";
		}
	}
	public static void main(String[] args) {
		int n = 100000;
		printTaxiCabNumbers(n);
	}

	private static void printTaxiCabNumbers(int n) {
		HashMap<Integer, Pair> map = new HashMap<Integer, Pair>();
		for(int i=0; i<n; i++){
			for(int j=i+1; j<n; j++){
				int m = (int) (Math.pow(i, 3)+Math.pow(j, 3));
				if(map.containsKey(m)){
					System.out.println(new Pair(i,j).toString()+" and "+map.get(m).toString());
				}else{
					map.put((int) (m), new Pair(i,j));
				}
			}
		}
	}
}

- settyblue June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

must it be in collection? or I can write solution while searching solutions.

public class AllSolutions {
    public static void main(String[] args){
        long maxint = (int)Math.pow(10,5);
        long powOfI=0;
        long powOfJ =0;
        for(int i =0;i<=maxint;i++){
            powOfI=(long)Math.pow(i,3);
            for (int j =i;j<=maxint;j++){
                powOfJ=(long)Math.pow(j,3);
                System.out.println("a && c = "+i+", b && d ="+j+", =>  a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
                System.out.println("a && d = "+i+", b && c ="+j+", =>  a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
                System.out.println("b && c = "+i+", a && d ="+j+", =>  a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
                System.out.println("b && d = "+i+", a && c ="+j+", =>  a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
            }
        }
    }
}

- pavelmehnin September 13, 2015 | 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