Google Interview Question for Software Engineer / Developers


Team: youtube
Country: United States
Interview Type: Phone Interview




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

I think doing % and / is not the correct approach to this, the algorithm basically work, but if we consider a number with millions of digits, the datatype should be some sort of linked list containing a single digit peer bucket, in that way the "big" number can grow to million digits, so in my opinion the correct way should be visit from back to beginning the linked list an put each digit into a string buffer then return it.

- vhmj1982 February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why back to beginning??

- neham October 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Can you provide the method's signature to perform this task? In what form is the integer provided?

- Will_In_Seattle January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the following program prints the decimal digits of any number from left to right:

def max_dig(n, l):
  prev=0
  next=1
  cnt=0
  while n>next:
    prev=next
    l.append(prev)
    next*=10
    cnt+=1
  return cnt

def prt_num(n):
  print n
  l=[]
  cnt=max_dig(n,l)
  while len(l)>0:
    x=l.pop()
    bits=4
    x<<=bits
    curr_dig=0
    while(bits>0):
      curr_dig<<=1
      x>>=1
      bits-=1
      if(n>=x):
        n-=x
        curr_dig+=1
    print curr_dig
def main():
  prt_num(5763453)

- gen-y-s January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how the integer presented to us? Could be its own datatype with huge memory used to store integers.
int lastdigit= number mod 10;
number = number/10;
use switch case to determine character value of lastdigit.
Store the character in a stack. Do this until the number is 0.
Empty the stack into a string.

- tushar January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}

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

Could you give an example.So that we can understand in a good manner

- Anonymous January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's like :
input - 12345678.......................8798989887873124234....(1 million digits)
output - "12345678.......................8798989887873124234...."
basically an itoa() kinda function but here the input is a very very large integer

- rcsingh81 January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well.. I also have a doubt on this question.. if it is what U explained above.. all you are doing is pointing to the memory location where the integer is and framing it to string or char *.

Probably the acsii string means an equivalent character string .. is it so ?

- hprem991 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we need to consider extended AScii codes too which goes till 255 or normal ascii which
spans till 127.For any of the case convert them into first that base number i.e for non-extended vesion, make 1 million digits to a number equivalent to base 128,and then group them from left.

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

in decimal or hex format string. It is easy for hex.

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

/* Basic prototype implementation in C.*/

#include<stdio.h>
#include <string.h>

void main() {
int number = 12345678;
int data = number;
int index = 0;
char ch[16];
char * chp = ch;
memset(chp, 0, sizeof(ch));

while(number != 0) {
*chp++ = number%10 + 48;
number = number/10;
index++;
}
printf("Ascii ver of number %d is \n", data);
for(index=15; index>=0 ; --index)
printf("%d ", ch[index]);
printf("\n");
return;
}

- M Khangura February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Read carefully: how would you convert this integer to an ASCII string
It does not say it has to be DEC string. You can just convert it to a HEX or OCT ASCII string.

- Larry February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

since the integer has millions of digits, we can divide it by 1000 or more, and base on the remainder of 1000, we can use this remainder as an index of our pre-stored string-array to get the corresponding string, it sacrifices the memory for time.

- shengzhc March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How will you represent 'this very very long integer'? is it going to be a bit string of size needed to represent a number?

32 bit represent a maximum of 10 digit number, so for a number of with 1 million digit what representation would be best?

One way could be to go for BCD representation, each byte representing a decimal from 0 - 9 or group of 4 bits representing a decimal from 0-9 (10,11,12,13,14,15 ignored).

In either case we need to know size of a byte stream representing a number. Now if we know number of bytes and way of representation we can compute size of a string needed beforehand. In first case, it will be same but for second it will be twice.

The way of conversion is now trivial. Just run through a byte array, interpreting entire byte or half-byte depending on a representation of long long type and then fill character array with it as usual ('0'+number)?

- confused_banda November 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let me know if you think this won't work:
suppose this data is saved in RAM in a big endian system, then part of this big number can be like "beef1234", what it is saved in RAM will be like this "be " take 1st byte, "ef" take 2nd byte,12 take 3rd byte, 34 take 4th byte, then

so we make a union:
union byteto4bit
{
unsigned char data;
struct 4bits{
top4bit:4;
btm4bit:4;
} Bits;
}
}

then we can access like this
while(i<sizeof(BigInteger))
{
union byteto4bit g.data=*(pBigInterger+i);
itoa(g.Bits.top4bit);
itoa(g.Bits.btm4bit);
}

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

void convert(long long number)
{
while(number)
{
long long tmp = nombre & 255;
cout << tmp << endl;
nombre = nombre>>8;
}
}

- khaled March 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think they are testing how good you know C.

char str[];
long long value =123456789101112;
sprintf(str,"%ld",value);

This solution should be sufficient.

- Ashish April 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I think this question is asking given a integer number, say 101102103, output "ABC", since A is 101 in the ascii table...

- Anonymous January 24, 2013 | 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