Google Interview Question


Country: India




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

gcd (int a, int b) {
   return  b == 0 ? a : gcd(b, a%b);
}

lcm (int a, int b) {
    return a * b / gcd (a, b);
}

- Sam February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
5
of 7 vote

First of all you need to clarify what you need to do if 0 is one of the arguments. I will just skip this case.

The gcd function below uses Euclidean algorithm. Do a search if you don't know it.

int gcd(int a, int b)
{
  while(b)
  {
    int tmp = b;
    b = a mod b;
    a = tmp;
   }
  return tmp;
}

int lcm(int a, int b)
{
  int gcd = gcd(a,b);
  return a*b/gcd;
}

- Rayden January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction it should be return a not return tmp.

- Rayden January 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we also need to check whether a<b or a>b in gcd computation.

- Anonymous April 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

LCM(m, n) X GCD(m, n) = m X n

Find GCD first, then...

LCM(m, n) = m X (n / GCD(m, n))

- mag January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

divide both numbers , update numbers with quotients(if remainder is 0) from number 2 to square root of the larger quotient remaining.

at each step multiply if any number divides any quotient with reminder 0, at last multiply the remaining quotients with the existing

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

int getLCM(int a, int b) {
int max = Math.max(a,b);
int counter = 2;
int x = max;
while(true) {
if(x%a ==0 and x%b == 0) {
return x;
} else { x = max * counter; counter++;}
}
}

- Yash January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
	int a=12,b=27,r;
	int t,m;
	int r1;

	t=(a>b)?a:b;
	m=a+b-t;
	
	r1=r=a*b;

	while(r>t)
	{
		r=r-t;
		if(r%m==0)
			r1=r;
	}

	printf("%d\n",r1);
	
	return 0;
}

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

This approach uses only minus operator to calculate gcd.

int gcd(int a,int b)
{
        int r;
        r=a>b?(a-b):(b-a);
        while(r)
        {
                a=b;
                b=r;
                r=a>b?(a-b):(b-a);
        }
        return a;
}
 
int main()
{
        int a=12,b=27,r;
        
        printf("%d ",a*b/gcd(a,b));
        return 0;
}

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

what about negative integers as inputs ? should we consider it ?

- Vivek July 08, 2012 | 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