Select chapter



functions (3.papers)
loops (1.papers)
arrays (2.papers)
structures (3.papers)
unions (1.papers)
pointers (7.papers)
null pointers (1.papers)
strings (2.papers)
misc c concepts (1.papers)
Data structures (5.papers)
linked lists (3.papers)
trees (1.papers)
basics (5.papers)
Hide

Sponsored Links


  

c interview questions and answers

1
Sample linked list program in c:

what is below disp() function doing?
what kind of data structure it is?

struct linkedlist{
    int val;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void disp(){
    struct linkedlist *temp = head;
    while(temp!=NULL){
        printf("%d ",temp->val);
        temp = temp->next;
    }
}
  1. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list.
  2. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list.
  3. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list, except for last node.
  4. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list, except for last node.
Show Answer
Share Facebook gmail Twitter
2
what is below disp() function doing?
what kind of data structure it is?

struct linkedlist{
    int val;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void disp(){
    struct linkedlist *temp = head;
    while(temp->next!=NULL){
        printf("%d ",temp->val);
        temp = temp->next;
    }
}
  1. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list.
  2. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list.
  3. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list, except for last node.
  4. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list, except for last node.
Show Answer
Share Facebook gmail Twitter
3
what is below disp() function doing?
what kind of data structure it is?

struct linkedlist{
    int val;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void disp(struct linkedlist *node){
    int cnt = -1;
    struct linkedlist *temp = head;
    while(temp != NULL && temp != node){
         temp = temp->next;
         cnt++;
    }
    if(temp == NULL)
    {
         printf("sorry");
    }else{
         printf("done..%d",cnt+1)
    }
    
}
  1. function disp() is trying to display all the elements of the given single linked list. after printing all the nodes values, it will print sorry all the times.
  2. function disp() is trying to find if a given node is available in this single linked list or not. if given node is available, then it is printing done with the index of that node in this linked list. assuming that starting node index is 0. else it prints sorry.
  3. function disp() is trying to find if a given node is available in this single linked list or not. if given node is available, then it is printing done with the index of that node in this linked list. assuming that starting node index is 1. else it prints sorry.
  4. function disp() is trying to find if a given node is available in this single linked list or not. if given node is available, then it is printing done with the index of that node in this linked list. assuming that starting node index is 0. else it prints sorry. But this logic wont work, because whether node is available or not, it always prints sorry only.
Show Answer
Share Facebook gmail Twitter
4
what is below fun() function doing?

struct linkedlist{
    int val;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void fun(struct linkedlist *node){
    struct linkedlist *temp = head;
    struct linkedlist *prev = temp;

    while(temp != NULL && temp != node){
         prev = temp;
         temp = temp->next;
    }

    if(temp != NULL && temp == head)
    {
         head = head->next;
         free(temp);
    }else if(temp != NULL){
         prev->next = temp->next;
         free(temp);
    }
    
}
  1. function fun () is trying to remove a given node in this single linked list if it is available. but this logic won't work for head and tail node.
  2. function fun () is trying to remove a given node in this single linked list if it is available. but this logic won't work for tail node.
  3. function fun () is trying to remove a given node in this single linked list if it is available. but this logic won't work for head node.
  4. function fun () is trying to remove a given node in this single linked list if it is available. it works for all the scenarios including head and tail node.
Show Answer
Share Facebook gmail Twitter
5
what is below disp() function doing?

struct linkedlist{
    int val;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

main(){
   disp(head);
}

void disp(struct linkedlist *temp){
    if(temp==NULL)
        return;
    disp(temp->next);
    printf("%d ", temp->val);
}
  1. this disp() function is trying to display the values of all the nodes from starting to ending node, by traversing through the single linked list using recursion technique.
  2. this disp() function is trying to display the values of all the nodes from ending to starting node (reverse order), by traversing through the single linked list using recursion technique.
  3. this disp() function is trying to display the values of all the nodes from ending to starting node (reverse order), by traversing through the single linked list using recursion technique. But this will not print starting node value.
  4. this disp() function is trying to display the values of all the nodes from ending to starting node (reverse order), by traversing through the single linked list using recursion technique. But this will not print ending node value.
Show Answer
Share Facebook gmail Twitter
6
How to delete a node in linked list in c:
If a pointer to a node in the single linked list is provided, then how to delete that node with out traversing through that linked list?
  1. With out traversing through the linked list, it is not possible to delete a give node, because when we are deleting that node, we have to make sure that previous node will point to the next node of current node. since we are not traversing, this is not possible.
  2. call free(node); and assign node=NULL; so that the node will be deleted successfully.
  3. since we have to delete node without traversing, we can copy the content of next node to current node, and delete the next node. But this trick will not work if the node supplied is the last node.
  4. we can free the current node, and change the previous node next pointer to point to next node of current node.
Show Answer
Share Facebook gmail Twitter
7
how to delete a node from doubly linked list in c:
If a pointer to a node in the double linked list is provided, then how will you delete that node with out traversing through that linked list.? Assuming that it is not a head or tail node.
  1. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        temp->prev->prev = temp->next;
        temp->next->next = temp->prev;
        free(temp);
    }
  2. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        temp->prev->next = temp->prev;
        temp->next->prev = temp->next;
        free(temp);
    }
  3. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
        free(temp);
    }
  4. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        free(temp);
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
    }
Show Answer
Share Facebook gmail Twitter
8
how to insert a node in a linked list:

what is below function insert() doing?
what kind of data structure it is?

struct linkedlist{
    int val;
    struct linkedlist *prev;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void insert(int item){
    struct linkedlist *temp = malloc(sizeof(struct linkedlist));
    temp->val = item;
    temp->prev = tail;
    temp->next = NULL;

    if(head == NULL){
        head = temp;
        tail = temp;
    }else{
        tail->next = temp;
        tail = temp;
    }
}
  1. function insert() is trying to insert a new node to the linked list at the end. head is pointing to starting node, and tail is pointing to last node. if there are no nodes both head and tail are having null values. this is an example of doubly linked list.
  2. function insert() is trying to insert a new node to the linked list at the end. head is pointing to starting node, and tail is pointing to last node. if there are no nodes both head and tail are having null values. this is an example of single linked list.
  3. function insert() is trying to insert a new node to the linked list at the head node. head is pointing to starting node, and tail is pointing to last node. if there are no nodes both head and tail are having null values. this is an example of doubly linked list.
  4. function insert() is trying to insert a new node to the linked list at the end. head is pointing to starting node, and tail is pointing to last node. if there are no nodes both head and tail are having null values. this is an example of double linked list.
Show Answer
Share Facebook gmail Twitter
9
Example code for traversing a linked list in c:

what is below function disp() doing?
what kind of data structure it is?

struct linkedlist{
    int val;
    struct linkedlist *prev;
    struct linkedlist *next;
}*head = NULL, *tail = NULL;

void disp(){
    struct linkedlist *temp = head;
    while(temp != NULL){
        printf("%d ", temp->val);
        temp = temp->next;
    }
}
  1. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list.
  2. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list.
  3. disp function is traversing through this doubly linked list, and printing the val of all the nodes present in this linked list, except for last node.
  4. disp function is traversing through this single linked list, and printing the val of all the nodes present in this linked list, except for last node.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
11
Will this below logic work for deleting a node in double linked list in c?

struct list{
  int no;
  struct list *prev;
  struct list *next;
};

void delete(struct list *temp)
{
    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    free(temp);
}
  1. Yes it will work for all nodes.
  2. It will not work for head node, but works for other nodes.
  3. it will not work for tail node, but works for other nodes.
  4. it will not work for head and tail node, but works for other nodes.
Show Answer
Share Facebook gmail Twitter
12
How to delete a given node from a doubly linked list in c, with out traversing?
Which of the below code will work for all the nodes?
  1. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        if(temp == head){
            temp->prev->next = NULL;
            head = temp->next;
        }else if(temp == tail){
            temp->next->prev = NULL;
            tail = temp->prev;
        }else{
            temp->prev->next = temp->next;
            temp->next->prev = temp->prev;
        }
        free(temp);
    }
  2. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        if(temp == head){
            temp->next->prev = NULL;
            head = temp->next;
        }else if(temp == tail){
            temp->prev->next = NULL;
            tail = temp->prev;
        }else{
            temp->prev->next = temp->next;
            temp->next->prev = temp->prev;
        }
        free(temp);
    }
  3. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        if(temp == head){
            temp->prev->next = NULL;
            head = temp->prev;
        }else if(temp == tail){
            temp->next->prev = NULL;
            tail = temp->next;
        }else{
            temp->prev->next = temp->next;
            temp->next->prev = temp->prev;
        }
        free(temp);
    }
  4. struct list{
      int no;
      struct list *prev;
      struct list *next;
    };
    
    void delete(struct list *temp)
    {
        if(temp == tail){
            temp->next->prev = NULL;
            head = temp->next;
        }else if(temp == head){
            temp->prev->next = NULL;
            tail = temp->prev;
        }else{
            temp->prev->next = temp->next;
            temp->next->prev = temp->prev;
        }
        free(temp);
    }
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
15
To access a random element which data structure is faster?

i. array  [or]
ii. linked list
  1. Linked list is faster
  2. array is faster
  3. both will be same.
  4. it depends.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
17
Which data structure is faster: To insert a new element ?

i. sorted single linked list  [or]
ii. sorted array
  1. sorted array will be faster.
  2. sorted linked list will be faster
  3. we can't predict
  4. both will be same
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
19
Which data structure is faster to find or search an element?

i. sorted array [or]
ii. sorted linked list
  1. sorted linked list will be faster
  2. both will be of same
  3. sorted array will be faster.
  4. we can't predict
Show Answer
Share Facebook gmail Twitter
20
Which data structure is faster to search a data item? 

i. array [or] 
ii. linked list
  1. Linked list is faster
  2. array is faster
  3. both will be same.
  4. both will be slower.
Show Answer
Share Facebook gmail Twitter
Read more papers :