Microsoft Interview Question






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

This q haunted me for sometime as designing a BigInt class is tremendous effort.

for interview I would start with

string firstNum="11231234454567900707070978989678967896789";
string secondNum="11231234454567900707070978989678967896789";

Now write your own Adder (remember 3 rd grade mathematics)
Now write your own Subtract(again use same logic)

string Add(string n1,string n2)
string Sub(string n1,string n2)

Now you can easily write Multiply and Division routine.

By taking some more pain, you can extend it for decimal numbers.

There should exist a better and efficient method.
But this "MIGHT" save your day. :D

- pk July 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think linked list is better than string or arrays.

struct Node {
byte digit;
Node next;
}

For adding and subtracting, you just need to add a new node in the list that keeps the sum.
For multiplying and division, I would create 2 new lists where I keep the temporary results and the final.

- S July 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we have a use case for this?
Where can this be used?

- DashDash July 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

linkd list solution is cool

- prash July 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can see how the linked list solution would work if the least significant bit is at the head of the list for adding, subtracting and multiplying but how would the division work? What would the temporary list be?

- lmessi July 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class operation
{
public static void main(String[] args)
{
ArrayList<Integer> no1=new ArrayList<Integer>();
ArrayList<Integer> no2=new ArrayList<Integer>();
String n1="1231231231242342342342342342";
String n2="122342345657568678678562342342";
int c=0;
while(c<n1.length())
{
no1.add(Integer.parseInt(n1.substring(c,c)));
c++;
}
c=0;
while(c<n2.length())
{
no2.add(Integer.parseInt(n2.substring(c,c)));
c++;
}

add(no1,no2);
}
public static void add(ArrayList<Integer> no1, ArrayList<Integer> no2)
{int temp=0, c;
ArrayList<Integer> no3=new ArrayList<Integer>();
while(c<no1.length() && c<no2.length())
{
no3.add((no1.get(c)+no2.get(c)+temp)%10);
temp=(no1.get(c)+no2.get(c))/10;
}

if(c<no2.length())
while(c<no2.length())
{
no3.add((no2.get(c)+temp)%10);
temp=0;
}
if(c<no1.length())
while(c<no1.length())
{
no3.add((no1.get(c)+temp)%10);
temp=0;
}

}

- Aseem October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class operation
{
public static void main(String[] args)
{
ArrayList<Integer> no1=new ArrayList<Integer>();
ArrayList<Integer> no2=new ArrayList<Integer>();
String n1="1231231231242342342342342342";
String n2="122342345657568678678562342342";
int c=0;
while(c<n1.length())
{
no1.add(Integer.parseInt(n1.substring(c,c)));
c++;
}
c=0;
while(c<n2.length())
{
no2.add(Integer.parseInt(n2.substring(c,c)));
c++;
}

add(no1,no2);
}
public static void add(ArrayList<Integer> no1, ArrayList<Integer> no2)
{int temp=0, c;
ArrayList<Integer> no3=new ArrayList<Integer>();
while(c<no1.length() && c<no2.length())
{
no3.add((no1.get(c)+no2.get(c)+temp)%10);
temp=(no1.get(c)+no2.get(c))/10;
}

if(c<no2.length())
while(c<no2.length())
{
no3.add((no2.get(c)+temp)%10);
temp=0;
}
if(c<no1.length())
while(c<no1.length())
{
no3.add((no1.get(c)+temp)%10);
temp=0;
}

}

- Aseem October 02, 2010 | Flag Reply


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