Google Interview Question for Computer Scientists


Country: United States
Interview Type: In-Person




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

concat(n1,n2)

// Assuming there is no buffer overflow

1. x = number of digits in n2, i:e ceil(log to the base 10 of n2)
2. if(x ==0 ) x = 1;
3. return n1*x +n2

public class ConcatNums 
{
    public static int concatNums(int n1,int n2)
    {
            int m = (int)Math.ceil(Math.log10(n2));
            m = (int)Math.pow(10,Math.max(1, m));

            return n1*m + n2;
    }

    public static void main (String args[])
    {
            int arr[] = {123,45,6789,10112,1,345,99,0};

            for(int i=1 ;i <arr.length;i++)
            {
                    System.out.println("Concatenation of " + arr[i-1] + " and " + arr[i] + " is " + concatNums(arr[i-1], arr[i]));
            }
    }
    
}

Output:
Concatenation of 123 and 45 is 12345
Concatenation of 45 and 6789 is 456789
Concatenation of 6789 and 10112 is 678910112
Concatenation of 10112 and 1 is 101121
Concatenation of 1 and 345 is 1345
Concatenation of 345 and 99 is 34599
Concatenation of 99 and 0 is 990

- hmm December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not work if n2 is 10^n (where n is integer).
Use Math.floor(Math.log10(n2) + 1)

- Marton January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

From Mathworld site:
" The formula for the concatenation of numbers p and q in base b is
p∥q=pb^(l(q))+q,

where
l(q)=|_log_bq_|+1 "
This is a solution in constant time..

- Pradeep January 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

calculating b^(l(q)) will be O(n)

- User123 January 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Correct Solution.

public static void concatenateNumbers(long x, long y) {
        long temp = y;
        do {
            temp = temp / 10;
            x = x * 10;
        } while (temp > 0);
        System.out.println(x + y);
    }

- techpanja January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hi all. I see a lot of O(n) solutions but this problem can actually be solved in O(log n) time.

public int concatInts (int a, int b) {
  return a * findMostSigDig(b, 10) + b;
}

/* Notice in this following method, I am not iterating through b linearly but exponentially therefore you get a log n solution */
public int findMostSigDig (int b, int prev, int pos) {
  /* if pos is currently less than most significant digit */
  if (b/pos > 0) {
    return findMostSigDig(b, pos, pos*pos);
  }

  /* if pos is currently at one position greater than the most significant digit
  if (b/(pos/10) > 0) {
    return pos;
  }

  /* if we have overshot pos, recurse with smaller subset */
  return findMostSigDig(b / prev, 1, 10) * prev;  
}

This code has NOT been tested and can be full of bugs but this is the general idea. Solution may change depending on whether negatives are allowed or what the range of ints are.

- jyimmer January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry the return statement for concatInts should actually be something like:
return a * findMostSigDig(b, 1, 10) + b;
Forgot a parameter in there. Can be initialized as 0 or 1 or even -1 but this value currently does not matter.

- jyimmer January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Last update. I went ahead and tested the code and it works. Once again the final solution is:

/* if pos is currently less than most significant digit */
  if (b/pos > 0) {
    return findMostSigDig(b, pos, pos*pos);
  }

  /* if pos is currently at one position greater than the most significant digit*/
  if (b/(pos/10) > 0) {
    return pos;
  }

  /* if we have overshot pos, recurse with smaller subset */
  return findMostSigDig(b / prev, 1, 10) * prev;

- jyimmer January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 8 vote

The second number in your example is out of range of int type in java.
I assume that the result and numbers are in range of int type:
Code:

private static void concat(long a, long b) {

	long temp = b;
	while (temp > 0) {
	    temp /= 10;
	    a *= 10;
	}
	a += b;
	System.out.println(a);
    }

Time complexity O(m) - m number of digits in sec number.

- thelineofcode December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's completely possible for this to be a Google interview question. It could have been a warmup or screening question.

- Anonymous January 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

This solution fails for following input: concat(123, 0)

- techpanja January 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

github.com/techpanja/interviewproblems/tree/master/src/numberproblems

public static void concatenateNumbers(long x, long y) {
        long temp = y;
        do {
            temp = temp / 10;
            x = x * 10;
        } while (temp > 0);
        System.out.println(x + y);

}

- techpanja January 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I wrote a O(log n) algorithm below where n is the number of digits in int b.

- jyimmer January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You just need to return 10 a when b == 0.

- Ehsan January 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a=18965;
int b=7852;
String s=Integer.toString(a)+Integer.toString(b);
System.out.println(Integer.parseInt(s));

- Kannan December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stringstream>

int concatenation(int i1, int i2){
	
	stringstream ss;
	ss << i1;
	ss << i2;

	int result;
	ss >> result;

	return result;
}

- HelloCPP December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An response in Go

package main

import "fmt"

func main() {
  b:= []int{1,1,2,2,2,3,3,4,5,7,15,15,15,15,15,17}
  max_count := 1
  count := 1
  max_value := b[0]
  for i:=1; i < len(b) ; i++ {
    // We increment the count if the same value
    if b[i-1] == b[i] {
      count = count + 1
    }
    if b[i] != b[i-1] {
      count = 1
    }
    if count > max_count {
      max_count = count
      max_value = b[i]
    }
  }
  fmt.Println("Total value:", max_value, "appears",max_count,"times")
}

- Elie January 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry wrong exercise ...
Not sure how to remove it though

- Elie January 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in Java. I don't know that much of complexities thing.
If anyone can provide me some good tutorials regarding algorithm complexities that will be a great help.

public static int concatInt(int x,int y)
	{
    	int len=String.valueOf(x).length();
    	return((int)(x*(Math.pow(10,len))+y));
	}

- aakash12121987 January 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

String.valueOf(x) is O(n)

- kuroobi January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not toString and then concat two string?

- shrimpy January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

String.concat() is O(n), so it's not fastest

- Anonymous January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time complexity depends on rank.
We can find rank using binary search because int has manixum number and minimun number. In this case, time complexity is log(rank(b))

int pow10(int n){ // Return value can be pre caluclated
	return (int) Math.pow(10,(double) n);
}

int rank(int a, int left, int right){
	if(right == left){
		return right;
}
if(right > left){
		return -1;
}
m = (right - left) / 2;
if( a < pow(10, m){
	return rank(a, left, m);
}else{
return rank(a, m, right);
}

}

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

range of integer is only from 0 to 4294967295 then how can the variable b and c hold values 78521369741 and 1896578521369741 respectively that are declared as integers without proper typecasting?

- Shana Bismi P.M. January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we can restrict the arguments to positive integers (likely), this can be done very simply.

The trick is to use a logarithm:

public static int concat(int a, int b) {
		return (int) (a * Math.pow(10, Math.ceil(Math.log10(b))) + b);
	}

Not sure what the runtime is since I don't know a lot about how log10 and pow are implemented. I'd guess that it's logarithmic, or near-constant since we are just dealing with relatively small variations in the problem size.

- gus January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So as i understood fomula is next:
O(n) = (a * 10 ^ (ceil(log10 b))) + b
O(n) = (a * 10 ^ (log10 b))
O(n) = 10 ^ (log10 b) // accroding to formula remove log
O(n) = b
O(n) = 1,

Or not?

- Andrew December 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose we have two integer

int i1 = 9890780787890
int i2 = 8908908

Size of integer in Java is 4 bytes.

The standard Java integer data types are:
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647 (- 2.14 Billion and -2.14 Billion)
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

How can concatenate these two.

Approach 1

Simple, No big deal .. one way is use String and sum this two.

Mean time is

Mean: 052
Approach 2
Multiplying the first one with 10X where X is number of zeros and sum.

Mean with this approach is
Mean 1490

import java.util.Random;
import java.util.Timer;

public class FastestIntConcatenation {

private static Timer timer ;
private static long mean;
private static int iterationCnt=1;
private static long timeCalculator(int i1, int i2, int approachFlag)
{

long startTime = System.nanoTime();
if(approachFlag == 1) approach1(i1, i2);
if(approachFlag == 2) approach2(i1, i2);
//if(approachFlag == 3) approach3(i1, i2);
long endTime = System.nanoTime();
long diff = endTime - startTime;
mean = (mean * iterationCnt + diff) /(iterationCnt + 1);
iterationCnt = iterationCnt + 1;
//System.out.println(("Diff:" + diff + ", mean:" + mean));


return mean;

}

private static String approach1(int i1, int i2)
{
return i1 + "" + i2;

}
private static void approach2(int a, int b)
{

long temp = b;
while (temp > 0) {
temp /= 10;
a *= 10;
}
a += b;
//System.out.println(a);
}
public static void main(String[] args) {

timer = new Timer();
int i1 = 23232;
int i2 = 1313213;
Random r = new Random();
//approach2(i1, i2);
for (int i = 0; i < 1000; i++) { i1 = r.nextInt(); i2 = r.nextInt();

System.out.println("Mean 0" + timeCalculator(i1, i2,0)); }

System.out.println("Approach 2 Starts");
for (int i = 0; i < 1000; i++) { i1 = r.nextInt(); i2 = r.nextInt();

System.out.println("Mean 1" + timeCalculator(i1, i2,1)); }

}

}

- anshuman101 February 22, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.


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