Microsoft Interview Question






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

Any clues on how to do it???

- Pavan B October 27, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First, create a data structure to represent the BigInt. Then, encapsulate the representation...

For representation, use an aggregate of chars(or bytes). Then, implement arithmetic on base-256. Depending on size of BigInts in arithmetic, sign extension may be necessary.

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

I'm curious, how much time was given for this question?

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

http://camino.rutgers.edu/ut/utsa/cs1723/lecture1.html

- Anunay November 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the link.

- AD August 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the clue how to do this:
(refer to gmplib.org for mature implementations)

// basic data type
typedef unsigned long limb;

class BigInt {

   limb *data;  // stores digits
   int n_limbs; // # of limbs (radix 2^32 digits)
   int n_allocated; // # of limbs allocated
   bool sign; // sign
   
public:
   BigInt() :
         data(0), n_limbs(0), n_allocated(0), sign(0) {
   }

   // constructs bigint from string
   BigInt(const char *str);

   // from integer
   BigInt(int x) :
           n_allocated(0), data(0) {
       alloc_limbs(1);
       data[0] = abs(x);
       sign = (x < 0);
   }  

   // adds to bigints: need special handling for signed operands
   friend BigInt operator +(const BigInt& a, const BigInt& b) {
       
       BigInt res;
       int na = a.n_limbs, nb = b.n_limbs;

       limb *pl = a.data, *ps = b.data, maxn = na, minn = nb; 
       if(nb > na) {
           swap(pl, ps);
           swap(maxn, minn);
       }
 
       res.n_limbs = maxn + 1;
       res.alloc_limbs(res.n_limbs);
       
       limb *pc = res.data;
       int carry = 0;
       for(int i = 0; i < minn; i++) {
           
           limb x = *pl++, c = x + *ps++;
           carry = (c < x) + carry; // adjust for carry
           c += carry;
           carry = (c < carry); // overflow if sum < summands
           *pc++ = c; 
       }

       // iterate over the high-order digits to update carry flag
       for(int i = 0; i < maxn - minn; i++) {
            limb c = carry + *pl++;
            carry = (c < carry);
            *pc++ = c;
       }
       *pc = carry; // add the leading carry 
   }

private:
    
   // allocates space enough to store n limbs
   void alloc_limbs(int n) {
       if(n_allocated >= n)
           return;
       
       data = (limb *)malloc(n);
       n_allocated = n; 
   }
     
};

- pavel.em June 12, 2008 | 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