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. |
An array X[n] stores names only. The first position of the array does not store a name rather it stores the number of available free spaces in the array. Write an algorithm to insert or delete an ITEM (accept it from the users) in the given array. |
|
Answer» Insert an ITEM: 1. ctr=0 /*Initialize the counter */ 2. If LST=n then { print "Overflow:" Exit from program } 3. if X[ctr]>ITEM then pos=1 else { 4. Repeat steps 5 and 6 until ctr>=U 5. if X[ctr]<=ITEM and ITEM<=X[ctr+1] then { pos=ctr+1 break } 6. ctr=ctr+1 7. ctr=n then pos=n+1 } /* end of if step 3*/ /* shift the elemets to create space */ 8. ctr=10 /*Initialize the counter */ 9. while ctr>=pos perform steps 10 through 11 10. { X[ctr+1]=X[ctr] 11. ctr=ctr-1 } 12. X[pos]=ITEM /* Insert the elements */ 13. END. Delete an ITEM 1. ctr=0 2. If n=0 { print "Underflow" Exit from program } 3. Repeat steps 4 and 5 until ctr<n 4. if(X[ctr]==ITEM) return ctr return -1 5. if(pos!=-1) X[pos]=0; print "Zero (0) signifies the deleted element" /*After this the free space will be put in the end of array */ 6. ctr=pos 7. Repeat steps 6 and 7 until ctr>=5 8. X[ctr]=X[ctr+1] 9. ctr=ctr+1 /* End of Repeat*/ 10. END |
|
| 2. |
Suppose an array A[10] stores numeric values only. Write algorithms to(i) calculate the average of the values in A(ii) print the even numbers stored in A. |
|
Answer» (i) calculate the average of the values in A 1. Start 2. read A[i] 3. let i=0,sum=0,avg=0 4. add A[i] into sum and store it into sum 5. divide sum by 10 and store it in avg 6. check whether i is less than 10 if yes than increment i by 1 7. print avg 8. End (ii) print the even numbers stored in A. 1. Start 2. read A[i] 3. let i=0 4. check whether A[i]%2 is equal to 0 5. if yes than print A[i] 6. check whether i is less than 10 if yes than increment i by 1 7. End |
|