Interview Question

Verification Engineer Interview

how to find the second smallest number out of iven n

  integers
Answer

Interview Answer

3 Answers

0

quick sort

Interview Candidate on Jul 11, 2011
1

I think you wrong :)

Where N is the number of the integers, quick sort's complexity is: o( (N)log(N) )

I will show you haw can you do it in o(N)

You can do i by keeping 2 variables:

smallest
second_smallest

smallest = second_smallest = fist number

While (there are still numbers to iterate) {
    if (next_integer < smallest) {
        second_smallest = smallest
        smallest = next_number;
   }
}

The Dude on May 20, 2012
1

No, you need to also check for the second_smallest as well. For instance your code does not work for the sequence of "10,20,15".

Nima on Sep 13, 2012

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up