Interview Question
Country: India
Interview Type: In-Person
Try to use linked list.
Generally interviewers don't interested in common API, they are interested in algorithms. So, BigInteger not suitable. Array cannot be used too because interviewers interested in "unlimited" numbers, so only linked list is apropriate for this task.
Arrays are suitable if you reallocate them when necessary. You could imagine using something like an ArrayList.
Eugene, I can agree with you due to technical issues. But I'm still thinking that interviewers would like see something "linked list"-based. Maybe I'm wrong :)
P.S. You can easily find answer for this question at Laakmann's book "CRACKING THE CODING INTERVIEW, FOURTH EDITION", page 108
#include<stdio.h>
#include<stdlib.h>
unsigned int count()
{
static unsigned long int num=0;
char ch;
if((ch = getchar())!='\n'){
num = 1 + count();
}
ungetc(ch, stdin);
return num;
}
int main()
{
char *str;
unsigned int long num;
num = count();
printf("number of char in the string is %l", num);
str = (char*)malloc((num+1)*sizeof(char));
scanf("%s", str);
return 0;
}
with the help of following code we can store at most (2^32-2) numbers of single digit in a string.. and as this is a array so we can easily use it in our program and
Use integer array to represent the number. That is how BigInteger is internally implemented in java api.
- chandershivdasani September 20, 2012Take a look at the source code over here(Replace - by .) :
www-docjar-org/html/api/java/math/BigInteger.java.html