Google Interview Question
Software Engineer / DevelopersCountry: United States
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.
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?
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.
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.
#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;
}
use a 3-D array
int crazyLongData [nth_number_of_looooooong_data][(max_digits)/(sizeof(int)*8)][max_digits]
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.
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 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 !
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
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.
if we use an array so size of array is required bt in linked list we can store data till memory available ...
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