InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Accept a list containing integers randomly. Accept any number and display the position at which the number is found in the list. |
|
Answer» maxrange = input(“Enter Count of numbers: ’’) marks=[] flag=False for i in range(0, maxrange): marks. append(input(“ ?”)) number = inputfEnter number to be searched”) for i in range(0, maxrange): if marks [i]==number: print number,“found at position”,i flag=True if flag==False: print number, “not found in list” |
|
| 2. |
For a given list of values in descending order, write a method in Python to search for a value with the help of Binary search method. The method should return position of the value and should return -1 if the value not present in the list. |
|
Answer» def binarysrch (nums, x): high=len (nums) low=0 while low < high: mid=(low+high)/2 midval=nums [mid] if midval > x: low = mid + 1 elif midval < x: high = mid else: return mid return -1 |
|
| 3. |
What will be the status of the following list after the First, Second and Third pass of the selection sort method used for arranging the following elements in descending order? Note : Show the status of all the elements after each pass very clearly underlining the changes. 12,14, -54,64,90,24 |
|
Answer» 12 14 -54 64 90 24 Pass 1 90 14 -54 64 12 24 Pass 2 90 64 -54 14 12 24 Pass 3 90 64 24 14 12 -54 |
|
| 4. |
How is linear search different from binary search? |
|
Answer» 1. Binary search requires the input data to be sorted near search doesn’t. 2. Binary search requires an ordering comparison; linear search only requires equality comparisons 3. Binary search has complexity 0(log n); linear search has complexity O(n) as discussed earlier. 4. Binary search requires random access to the data; linear search only requires sequential access (this can be very important – it means a linear search can stream data of arbitrary size). |
|