Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

Friday, March 6, 2009

4. Position of a number in a sorted array

4. How many comparisons are required to find the position of a number in a sorted array.

Answer: Using binary search it would require O(log n) comparisons.

Saturday, February 21, 2009

find the k largest or smallest numbers from a set of n numbers

3. How do you find the k largest or smallest numbers from a set of n numbers

Solution:

Build a min/max heap of size K from the available set.

Time complexity: n Log K

Tuesday, February 17, 2009

Reverse a string

This is one of my favourite and has several correct solutions.

2. Reverse a string.

The best way would be to loop through half of the sting and swap the characters at either end (without using an extra variable!)