Google Interview Question for Software Engineer / Developers


Country: United States




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

Better way to store long integer data type data is array. So that we can use this number for any arithmetic operation. eg) if the number is 123456789, keep the data in the form of (1,2,3,4,5,6,7,8,9)

- JobHunter July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, char array is good idea as any operation can start from lowest digit and can continue to higher ones if there is a carry.

- Anonymous July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But if the data length is unknown, and we have to store the data first. And the LinkedList may cost a lot of memory... Any opinions?

- Sean September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just use vector<char>

- kingpotter1990 October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But internally vector is also an array.

- anonyguru November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
4
of 14 vote

We need max 4 bits to represent digits from 1 to 9.
Use a structure with bit-field.

struct b
{
        unsigned int num:4;
};

Use an array of such structure where each slot will be handling each digit.

This will conserve memory.

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

Good one!!

- Psycho July 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are allocating a 4-byte structure to store 4 bits. Even if you use the char type, you lose half the space.

If we want to save space, I think we have to figure out the range of the data and the number of bits required for it then define a structure containing the smallest array of 64-bit integers.

- oos September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

vector<int>, first element is the least significant part, following elements are added dynamically. It is relatively easy to implement most arithmetic operations

- Anonymous July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;

struct DT{
    unsigned int d:4;
};

int main()
{

    int size,digit;
    DT *dt;

    cout<<"Enter number of digits to store : ";
    cin>>size;
    dt = new DT[size];

    for(int i = 0; i<size; i++)
    {
        switch(i+1)
        {
            case 1 :
                cout<<"Enter "<<i+1<<"st digit from left : ";
                break;
            case 2 :
                cout<<"Enter "<<i+1<<"nd digit from left : ";
                break;
            case 3 :
                cout<<"Enter "<<i+1<<"rd digit from left : ";
                break;
            default :
                  cout<<"Enter "<<i+1<<"th digit from left : ";
        }

        while(true)
        {
            cin>>digit;
            if(digit>=0 && digit<10)
                break;
            cout<<"Renter this digit please  :";
        }
        dt[size-1-i].d=digit;
    }

    cout<<"Your number is : ";
    for(int i = 0; i<size; i++)
        cout<<dt[size-1-i].d;

    return 0;
}

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

Use Strings to represent long data.

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

use a 3-D array

int crazyLongData [nth_number_of_looooooong_data][(max_digits)/(sizeof(int)*8)][max_digits]

- softy July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i didn't understand how you are using a 3-D array....please explain it more.

- jaip42 July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question says : "data structure to capture long datatype" suppose the numbers are having some billion digits. How will you store them when you dont have a data type in c which can store large numbers.
so i am storing the digits from lsb till 32nd digit in row 1 of a 2 D array then in row 1 i am storing fomr 33 digit to 32 + 33rd digit and so on.I will be needing maximum (max_digits)/(sizeof(int)*8 = (max_digits)/32 rows, assuming intezer takes 32 bits.
so a'[m][n] represents the number and a[0][m][m] represents the first number.Does this make some sense?
But i think string would be the better way to store the large numbers.

- softy July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think a single one-dimensional array (or linked list in cases whr we dont know the maximum size of number) might be a better option..
I didnt understand the part whr u said that "How will you store them when you dont have a data type in c which can store large numbers."
U dont need a large number datatype here. The datatype would be a simple int. Its the size of the array/linked list which would be large.
When we know the size of maximum possible number, we can declare that sized array. If we dont know, then simply use linked list and keep increasing the size as needed.

- reader July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@reader ok then by your reasoning do we have to take the array of linked list structure, example by your approach we must have linked list for every number .So say if we have two numbers one is 5 and another is 5678.......upto 1 million digits so there will be two linked list one having one node having dat value = 5 and another having 1 million nodes represnting 5678...upto 1 million digits.or do you want to take only a single link list , but then how will you demarcate the next number.Plz debate !

- Anonymous July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anonymous on July 15, 2012 is = softy !

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

ArrayList in little endian format

- Anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think the best answer would be 'file' . Open a file and write to it the entire input and we can access any digit through the file pointer

- Anonymous July 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please elaborate a bit more?

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

struct data{
char ch[100]; // this will store 200 Digits
struct data *next;
};

if digit in 100 Multiple
overhead of memory= (200+ 2*4)/ 200= 1.04%

- Abhijeet Rokade August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about trie?

- Anonymous August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question is : how do you store suppose one 1000 digit long number..

The question is not for "how to you store 1000 of long numbers!!

- Psycho August 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Steps:
1. Count number of digit in the number(i.e. length of the number), Let it be N.
2. Create 10 Bit vectors(i.e. for each num "0, 1, 2, 3, 4,5 6, 7, 8, 9") of lenth N.
    BOOLEAN VECTORS[10][N] = {0};
3. For each number(i.e. "0, 1, 2, 3, 4,5 6, 7, 8, 9") in Large number, create boolean vector.
    for(int j=0; j<N; j++){
        m = MSB(LARGE_NUMBER);
		remove m from LARGE_NUMBER;
        VECTORS[m][i] = 1;
    }
4. For each number(i.e. "0, 1, 2, 3, 4,5 6, 7, 8, 9") compress the corresponding boolean vector.
    for(i = 0; i<10; i++)
        compress(VECTORS[i])
5. Now store the compressed vectors of the 10 digits of the number in HASH Table.

NOTE:
Data Structure Use Here
1. Bit Vector Representation for each number.
2. Hash Table to store all number.

- SRB August 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I like the bitfield approach, but what about converting the number to hexadecimal so that no bit patterns will be wasted. In decimal representation you have to waste 6 bit patterns per 4 bits.

- Riyad Parvez July 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I would say a balance binary tree structure like an AVL tree where everything happens in logarithmic time.

- D July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

using linked list, we can store the data till available memory.

- khushi July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think he means that.

- Krishna July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if we use an array so size of array is required bt in linked list we can store data till memory available ...

- Anonymous July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But for each digit, we need 4 bytes of address to be stored!

- Psycho July 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's ok !!! to use linked list but it's space and run time complexity will increase tremendously

- Arun Kumar Gupta July 24, 2012 | Flag


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