Microsoft Interview Question for Software Engineer in Tests






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

Can it not be used unsigned int with some condition like

if ( fact < UINT_MAX ) { return ( fact( b ) ); }
else { // overflow }

- nks December 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Once an unsigned int overflows, its value gets set to 0 so that would not work.

if (fact > 0) { return ... }
else { //overflow}

would be correct I guess for both signed and unsigned integers

- C December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually integer overflows are undefined behavior according to the C spec so the above method is not entirely correct (i.e. only gcc compiler specific).

- C December 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do something like this
int fact(int n)
{
int factorial =1;
for (int i=2;i<=n;i++)
{
if(Integer.MAXVALUE/factorial < i)
return factorial;
else
factorial = factorial * i;
}
return factorial;
}

- Nishant January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 #include <iostream>
2 #include <climits>
3
4 using namespace std;
5
6 unsigned factorial(unsigned n)
7 {
8 if(n==0||n==1)
9 return 1;
10 if(UINT_MAX/n < factorial(n-1))
11 return 0;
12 return factorial(n-1)*n;
13 }
14
15 int main()
16 {
17 cout << "factorial(4) is " << factorial(4) << endl;
18 cout << "factorial(10) is " << factorial(10) << endl;
19 cout << "UINT_MAX is " << UINT_MAX << endl;
20 cout << "factorial(20) is " << factorial(20) << endl;
21 }

- kyra January 29, 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