Sunday, April 21, 2013

Amazon Support Engineer - Online Test

Total 10 questions (7 MCQ & 3 Programming Questions)

7 MCQ questions includes the following sections:


1. Stacks & Queues
2. Min Heap
3. Linked List (circular Linked List)
4. DBMS Questions (DDL , dML)
5. OS questions (semaphore)
6. Aptitute Question
7. Given a program , what is the output ? (Given, virtual function questions)

3 Programming Questions:


8. Pattern Matching Question
9. Swap adjacent nodes in a linked list.
10. Write a script to print the uniq phone numbers in a folder which contains list of html files.
phone numbers can be in any format (xxxxxxxxxx , xxxxx-xxxxx , xxx-xxx-xxxx)

Online test conducted through Interviewstreet.com

Tuesday, March 26, 2013

Matrix Problems

Some matrix (2D array) problems

1. Given two matrices, perform Matrix Addition

2. Given two matrices, perform Matrix Subtraction

3. Given two matrices, perform Matrix Multiplication

4. Given two matrices, perform Matrix Division

5. Given a matrix , output the mirror of the matrix

6. Given a matrix , output 90 degree left rotation 

7. Given a matrix , output 90 degree right rotation

8. Given a matrix , make diagonal elements zero

9. Given a matrix , make elements zero except diagonal

10. Given a matrix , Make upper triangular matrix zero

11. Given a matrix , Make lower triangular matrix zero

12. Given two matrix , Check whether both are identical

13. Given a matrix , some element mXn is zero. Make the Mth row & Nth column zero.

14. Given a matrix, Adding upper & lower triangular matrix

15. Given a matrix , Swapping upper & lower triangular matrix elements

16. Given a matrix , Make the elements in the border are zero.

17. Given a matrix , Make the corner elements zero.

18. Given a matrix , perform binary search in a matrix

Sunday, March 17, 2013

Quick Sort



#include<stdio.h>
#define MAX 10
void qsort(int A[], int p, int r);
int partition(int A[], int p, int r);

main()
{
int Arr[MAX]={10,4,5,6,2,3,8,1,9,7};
int i;
qsort(Arr,0,MAX);

for(i=0;i<MAX;i++)
printf("%d ",Arr[i]);
}

void qsort(int A[], int p, int r)
{
int q;
if(p<r)
{
q=partition(A,p,r);
qsort(A,p,q-1);
qsort(A,q+1,r);
}
}

int partition(int A[], int p, int r)
{
int i , j , x,tmp;
x=A[r];
j=p;
i=p-1;
while(j<r)
{
if(A[j]<x)
{
i=i+1;
// Exchange A[i] & A[j]
tmp=A[j];
A[j]=A[i];
A[i]=tmp;
}
j++;
}
// Exchange A[i+1] & A[r]
tmp=A[i+1];
A[i+1]=A[r];
A[r]=tmp;

return i+1;
}

Output:
1 2 3 4 5 6 7 8 9 10

Time Complexity:
Best - O( n log n )
Average - O(n log n )
Worst - O( n^2)

Pair Wise Swapping in a Linked List


#include<stdio.h>
#include<stdlib.h>

typedef struct list
{
int info;
struct list *next;
}node;

node * head;

void insert(int data);
void display();
void pairwise_swapping(node *ptr);
void swap(int *a, int *b);

void insert(int data)
{
node *ptr , *tmp;
ptr=(node*)malloc(sizeof(node));

if(ptr==NULL)
return;

(*ptr).info=data;
(*ptr).next=NULL;
if(head==NULL)
{
head=ptr;
}
else
{
tmp=head;
while((*tmp).next!=NULL)
tmp=(*tmp).next;

(*tmp).next=ptr;
}
}

void display()
{
node * ptr;

if(head==NULL)
{
printf("list is empty\n");
return;
}

for(ptr=head;ptr!=NULL;ptr=(*ptr).next)
printf("%d ",(*ptr).info);
}

void swap(int *x , int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

void pairwise_swapping(node *ptr)
{
node *tmp , *tmp_next;
tmp= ptr;
while (tmp!=NULL && (*tmp).next!=NULL)
{
tmp_next = (*tmp).next;
swap(&(*tmp).info , &(*tmp_next).info);
tmp=(*tmp_next).next;
}
}

main()
{
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
pairwise_swapping(head);
display();
}

Output:
2 1 4 3 6 5 

Friday, March 15, 2013

Merge Sort

#include<stdio.h>
#define MAX 10

void MergeSort(int Arr[],int low, int high);
void Merge(int Arr[],int low, int mid,int high);

main()
{
    int A[MAX]={9,8,7,6,5,4,3,2,1,10};
    int i;
    MergeSort(A,0,MAX-1);
    for(i=0;i<MAX;i++)
    printf("%d ",A[i]);
}

void MergeSort(int Arr[], int low, int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        MergeSort(Arr,low,mid);
        MergeSort(Arr,mid+1,high);
        Merge(Arr,low,mid,high);
    }
}

void Merge(int Arr[], int low, int mid, int high)
{
    int l,m,i,k;
    int temp[MAX];

    l=i=low;
    m=mid+1;

    while((l<=mid) && (m<=high))
    {
        if(Arr[l]<=Arr[m])
        {
            temp[i]=Arr[l];
            l++;
        }
        else
        {
            temp[i]=Arr[m];
            m++;
        }
        i++;
    }

    if(l>mid)
    {
        for(k=m;k<=high;k++)
        {
        temp[i]=Arr[k];
        i++;
        }
    }
    else
    {
        for(k=l;k<=mid;k++)
        {
            temp[i]=Arr[k];
            i++;
        }
    }

    for(k=low;k<=high;k++)
    Arr[k]=temp[k];
}

Time Complexity: O(n logn)

Insertion Sort ( reverse)

 

Orginal insertion sort will sort elements from starting of the array. This program will sort the elements from end of the array.

/*Insertion Sort*/

#include<stdio.h>
void insertion(int A[], int n);
void print_array(int A[],int n);
main()
{
    int arr[]={7,6,5,4,2,3,1,8,10,9}; // Input Array

    int size = (int)(sizeof(arr)/sizeof(arr[0])); //Calculate size of array

    int itr ; // Iterator

    insertion(arr,size); // Insertion function

    print_array(arr,size);
}

void print_array(int A[], int n)
{
    int itr;

    // Printing array after sorting
    for(itr=0;itr<n;itr++)
    printf("%d ",A[itr]);
}

void insertion(int A[], int n)
{
    int i , j , key;
    for(j=n-1;j>=0;j--)
    {
        key=A[j];
        i=j+1;
        while((i<=n)&&(A[i]<key))
        {
            A[i-1]=A[i];
            i=i+1;
        }
        A[i-1]=key;
    }
}