RelQ Software Company Limited Interview Question for Testing / Quality Assurances






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

#python version
def search(arr,start,end,val):
retVal = (False,-1)
if ( start > end):
return retVal
mid = (start + end)/2
if ( val == arr[mid]):
retVal = (True,mid)
print "Found at %d"%mid
else:
if ( start == end):
print "Val %d does not exist "%val
elif ( arr[mid] > val):
retVal = search( arr , start , mid - 1 , val)
else:
retVal = search( arr , mid + 1 , end , val)

return retVal

- Mac February 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a C version (return 0 if target is not found in arr):
int binary_search(int arr[], int init, int last, int target)
{
int intermidiate=init+floor(double(double(last-init)/double(2)));
if(last-init>0)
{
if(target==arr[intermidiate])
return intermidiate;
else
{
if(target<arr[intermidiate])
return binary_search(arr, init, intermidiate, target);
else
return binary_search(arr, intermidiate+1, last, target);
}
}
else if(last-init==0)
{
if (arr[last]==target)
return last;
else return 0;
}
else
return 0;

}

- fengyu225 March 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code is WRONG. Try searching array {2} for target 1.
First iteration: init = 0, last = 0, intermediate = 0
Second iteration: init = 0, last = 0, intermediate = 0
... infinite loop!

- bob April 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BinarySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[6] { -10, -2, 0, 4, 6, 10 };
            int index = ReturnIndex(array, 6, 0, array.Length-1);
            if (index == -1)
            {
                Console.WriteLine("Element not found");
            }
            else
            {
                Console.WriteLine("Located at position {0}", index);
            }
            Console.Read();
        }
        public static int ReturnIndex(int[] array, int num, int start, int end)
        { 
            if (end < start)
            {
                return -1;
            }
            else
            {
                int mid = end - start / 2;
                if (num > array[mid])
                {
                    return ReturnIndex(array, num, mid + 1, end);
                }
                else if (num < array[mid])
                {
                    return ReturnIndex(array, num, start, mid - 1);
                }
                else 
                {
                    return mid;
                }
                
            }
        }
    }
}
Output:
Located at position 4

- Vijetha January 16, 2013 | 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