Showing posts with label Data Structure Questions. Show all posts
Showing posts with label Data Structure Questions. Show all posts

Thursday, September 13, 2012

Find the maximum and minimum of given numbers

pair the numbers in groups of two compare them and winner need not be checked for minimum and loser for maximum thus we would get maximum and minimum in
3n/2 -2 comparison

for example let us have an array of a[]={34,21,45,67}
first compare 34 and 21 winner is 34 loser 21

second compare 45 67 winner is 67 loser 45
third compare 67 and 34 (winners of first two) winner is 67 no need to maintain loser
fourth compare 21 45 (losers of first two) loser is 21 no need to maintain winner
we have found the winner is 67 and loser is 21 in just four comparison. You can try it on more numbers


Find smallest element in rotated array

#include<stdio.h>


int find_smallest_element_position(int *a, int first , int last);

main()

{

int a[10]={4,5,6,7,8,9,1,2,3};

int pos;

pos=find_smallest_element_position(a,0,8);

printf("The smallest element's position is %d\n",pos);

}



int find_smallest_element_position(int *a, int first , int last)

{

int mid;

while(first&lt;last)

{

mid=(first+last)/2;

if(a[mid]&gt;a[last])

{

first=mid+1;

}

else

{

last=mid;

}

}

return first;

}

Find an element in sorted matrix

Questions : you have a [n * n] array which is sorted both row wise and column wise. How do you find an element in this array in O(n) time ?

Answer 1.-

Let X be the bottom left element of the matrix and we need to find the position of an element K.
Now if K = X, we can return the position of X.
Else if K < X, then we can possibly eliminate that row as all the elements in that row are greater than or equal to X.
Else if X < K, then we can eliminate that column as all the elements in that column are lesser than X.
Now in each step, we eliminate either a row or coll which finally leads to that element.



Difference between HashMap and Hashtable

The iterator in the Hash Map is fail-safe (If you change the map while iterating, you’ll know). The enumerator for the Hash table is not fail-safe.Hash Map is unsynchronized.Hash table is synchronized.



Can you store multiple data types in System.Array

No
Note-( array is collection of same data type)

Can we create an object for an interface

Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.


Can constructor be inherited

No, constructor cannot be inherited, though a derived class can call the base class constructor.

  • Iterator is an Interface implemented a different way for every Collection.
  • Adding two linked list

    you have two linked list for two decimal numbers means

    if there r two no..........3627809 & 234
    two linked list will be

    1.  3->6->2->7->8->0->9->NULL

    2.  2->3->4->NULL

    your output linked list will be

    3->6->2->8->0->4->3->NULL
    Answer -
    First reverse both the lists and then add. After adding reverse the resulted linked list. Code is as follows -
    linked list p,q, result;
    
    
    
    int x=0,num;
    
    
    
    p=reverse(p);
    
    q=reverse(q);
    
    
    
    
    
    while(p!=NULL  || q!=NULL)
    
    {
    
     num=p-&gt;data+q-&gt;data+x;
    
     
    
     result-&gt;data=num%10;
    
     x=num/10;
    
     
    
     p=p-&gt;link;
    
     q=q-&gt;link;
    
    }
    
    if(p!=NULL)
    
    {
    
     p-&gt;data=p-&gt;data+x;
    
     add their nodes at the back;
    
    }
    
    
    
    
    
    if(q!=NULL)
    
    {
    
     q-&gt;data=q-&gt;data+x;
    
     add their nodes at the back;
    
    }
    
    result=reverse(result);