# Interview Questions: Project 01

• Please post the Interview Questions and Answers related to Data Structures using C on Linux
• 1. Find the intersection and union of two linked list ( L1 = 1->2->3->4->5 ) and (L2 = 3 -> 5 ->6 ) and result put in third linked list (L3) ?
2. Sort the given string.
Input Output
This is test. ------->         ehiisssttt
Note: Remove spaces,comma,dot and other symbols.
• Ans of above Ques.
2. Sort the given string
int main()
{
char *buff,*temp;
int i=0,j=0,k=0,index=0;
char  smallest,tmp;

buff=(char*)malloc(sizeof(char)*20);
temp=(char*)malloc(sizeof(char)*20);   //temp mem to store the the string to sort

printf("Enter the string\n");
fgets(buff,100,stdin);
buff[strlen(buff)-1]='\0';                   //excludes the \n placed in the scanned string
printf("string length is %d\n",strlen(buff));
printf("string entered is %s\n",buff);
while(*(buff+i)!='\0')
{
if(*(buff+i)>=97 && *(buff+i)<=122)        //to preserve small letters
{
*(temp+k++)=*(buff+i);
}

if(*(buff+i)>=65 && *(buff+i)<=90)        //to make uppercase letter to lowercase and preserve it
{
*(temp+k++)=*(buff+i)+32;
}
i++;
}
*(temp+k)='\0';
printf("string to sort is %s\n",temp);     //final string to sort (spaces,'.',and other special characters removed)

for(i=0;i<strlen(temp);i++)
{
smallest=*(temp+i);
index=i;
for(j=i+1;j<strlen(temp);j++)
{
if(*(temp+j)<smallest)
{
smallest=*(temp+j);
index=j;
}
}
tmp=*(temp+i);
*(temp+i)=*(temp+index);
*(temp+index)=tmp;
}
strcpy(buff,temp);
printf("sorted string is %s\n",buff);
return 0;
}
~
• Q1.Modified:
Given two link lists say L1: 1->2->3->4->6->null and L2: 2->4->6->8->null.
L3:output list should be L3: 2->4->6->null (intersection nodes of L1 and L2)
We have to first take input in manner:
->ask for the elements in first list
->take input of first 1 2 3 4 6 separated by spaces
->ask for the elements in second list
->take input of second 2 4 6 8 separated by spaces
->output the resultant list L3: 2->4->6->null

• main
{
int i,size1,size2;
int *arr1,*arr2;

printf("Enter the size of list1\n");
scanf("%d",&size1);
printf("Enter the size of list2\n");
scanf("%d",&size2);

arr1=(int*)malloc(sizeof(int)*size1);
if(!arr1)
perror("malloc");

arr2=(int*)malloc(sizeof(int)*size2);
if(!arr2)
perror("malloc");

printf("Enter elements of list 1 in arr1\n");
for(i=0;i<size1;i++)
{
scanf("%d",arr1+i);
}
printf("Enter elements of list 2 in arr2\n");
for(i=0;i<size2;i++)
{
scanf("%d",arr2+i);
}

for(i=0;i<size1;i++)
{
if(i==0)
{
}
else
{
new=createNode();
new->data=*(arr1+i);
}
}

for(i=0;i<size2;i++)
{
if(i==0)
{
}
else
{
new=createNode();
new->data=*(arr2+i);
}
}

printf("list1 is \n");
printf("list2 is \n");

printf("list3 is \n");

return 0;
}

{
node *temp=NULL;
temp=result;
if(temp==NULL)
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
}
return temp;
}
{
node *result,*temp=NULL;
{
else
else                        //equal
{
}
}
return 0;
}

node* createNode()
{
node *crnode=NULL;
crnode=(node*)malloc(sizeof(node));
if(!crnode)
perror("malloc");
crnode->next=NULL;
return crnode;
}

{
node *temp=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
return 0;
}

{
node *temp;
printf("strt->%p\n",temp);
while(temp!=NULL)
{
printf("data->%d & next->%p\n",temp->data,temp->next);
temp=temp->next;
}
return 0;
}

• Q. Reverse the linked list without recursion (using one loop)
A.

curr=strt;
fwd=curr->next;
fwdFwd=fwd->next;
curr->next=NULL;            //last node is pointing to null

while(fwdFwd)
{
fwd->next=curr;
curr=fwd;
fwd=fwdFwd;
fwdFwd=fwd->next;
}

fwd->next=curr;
curr=fwd;                           //last node of original list becomes the current node

printf("list reverse without recursion is:\n");
displayList(curr);