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 linked lists interview questions and answers

1
Example for typedef struct in c:

What is the problem with below typedef structure declaration? if there is no problem, then what is it declaring?
typedef struct node {
                char *item;
                struct node *next;
} *NODEPTR;
  1. it is a declaration for single linked list structure. There is no problem with this declaration.
  2. it is a declaration for single linked list structure.we can't use struct node with in the definition of struct node, it gives compile time error.
  3. it is a declaration for single linked list structure.we can't use struct node with in the definition of struct node, it gives run time error.
  4. none of the above are true.
Show Answer
Share Facebook gmail Twitter
2
What is the output of below C program?

#include  <stdio.h>
    
typedef struct{
    int sno;
    char *sname;
    struct node * next;
}* NODE

main(void)
{
    printf("hi");
    return 0;
}
  1. hi
  2. it might do crash dump at run time and close the app, main is looking like it returns a struct.
  3. no error no output
  4. compile time error.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
linked list example in c:

what is below function insert() doing?
is it a single linked list or double linked list?

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

void insert(int item){
    struct linkedlist *temp = malloc(sizeof(struct linkedlist));
    temp->val = item;
    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
5
example program for singly linked list in c:

In the below program in insert() method,
what will happen if temp->next = NULL; statement is removed?

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

void insert(int item){
    struct linkedlist *temp = malloc(sizeof(struct linkedlist));
    temp->val = item;
    temp->next = NULL;
    if(head == NULL){
        head = temp;
        tail = temp;
    }else{
        tail->next = temp;
        tail = temp;
    }
}
  1. Nothing will happen, it will work fine.
  2. Nothing will happen, because any way we are setting tail->next = temp; so nothing wrong even if we remove that temp->next=NULL; statement.
  3. if temp->next=NULL; is removed then it is difficult to find out where is the end for this linked list. and it might even crash when we try to move to next node after tail node.
  4. if temp->next=NULL; is removed then it is difficult to find out where is the end for this linked list. But it is not impossible also, because we are having tail pointing to last node, so while retrieving we have to check if next node is equal to last pointer, to stop proceeding further.
Show Answer
Share Facebook gmail Twitter
6
singly linked list in c:

What is the problem with below program?

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

void insert(int item){
    struct linkedlist *temp = NULL;
    temp->val = item;
    temp->next = NULL;
    if(head == NULL){
        head = temp;
        tail = temp;
    }else{
        tail->next = temp;
        tail = temp;
    }
}
  1. it will give compile time error, because temp is assigned with NULL and trying to dereference it in the next line.
  2. it will crash at run time, because with out allocate memory to temp, we are trying to assign values to that node. which is potentially doing a null pointer dereference.
  3. we can assign values to a NULL linked list node, but it will be ignored, so there is no error in this program.
  4. it will give linking time error, as we are tyring to do null pointer reference with temp pointer in insert() function.
Show Answer
Share Facebook gmail Twitter
7
How to find a middle element of a linked list in c?
  1. This function uses one slow and one fast
    pointer to get to the middle of the linked list.
    
     The slow pointer is advanced only by one node
     and the fast pointer is advanced by two nodes! Once fast moving pointer reaches end, slow moving pointer will be at middle node.
  2. find the size of the linked list(i.e no of nodes in the linked list). and directly move to the middle node, and return that node pointer.
  3. while creating a linked list, maintain an array of pointers, which will have pointers to all the nodes. If we want find the middle node, then directly access array[middle-1] element to get the address of the middle node.
  4. We can use either option 1 or option 2.
Show Answer
Share Facebook gmail Twitter
8
What is the difference between array and linked list in c?
  1. both are same, only difference is linked list uses pointers and arrayes don't.
  2. linked list stores elements sequentially, array holds elements randomly in the memory with a link between them.
  3. array stores elements sequentially, linked list holds elements randomly in the memory with a link between them.
  4. none of the above
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
10
Sample code for linked list structure in c:

What is the problem with below linked list  declaration? if there is no problem, then what is it declaring?

typedef struct linked_list{
        int sno;
        char *sname;
        NODE next;
}* NODE;
  1. it is a typedef declaration for single linked list structure. There is no problem with this declaration.
  2. it is a typedef declaration for single linked list structure. typedef NODE is being used before being declared, so it will give compile time error.
  3. it is a typedef declaration for  single linked list structure. it gives run time error, invalid declaration.
  4. none of the above are true.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
12
typdef linked list in c:

What does LINKEDLIST reffer to in below code snippet?

typedef struct linkedlist{
  int sno;
  char *sname;
  LINKEDLIST prev;
  LINKEDLIST next;
}* LINKEDLIST;
  1. it is a pointer to a linked list
  2. it is typedef to a structure.
  3. it is typedef to a pointer to a structure.
  4. it is a single linked list.
Show Answer
Share Facebook gmail Twitter
13
How to search data in a linked list in c?
  1. We can search a linked list using binary search technique directly. Since linked list nodes are allocated in contiguous memory location, binary search works on linked list also.
  2. the only way to search linked list is, dump the data into array of structures & apply binary search technique on the arrays.
  3. The only way to search a linked list is with a linear search, because the only way a linked list’s members can be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure like array, so that searches can be more efficient (like binary search).
  4. all of the above mentioned techniques can be used for searching a linked list.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
16
How to find if a node or pointer is corrupted in the linked list?
  1. Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it).
  2. It is good programming practice to set the pointer value to NULL immediately after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required.
  3. Use a good debugger to see how the data structures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily.
  4. all of the above
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
18
Is it possible to do binary search on a linked list?
  1. Yes, but it doesn't really make sense to have binary search on a linked list as moving to middle node logic will not be straight forward. For this we should have pointers of all nodes stored in an array. Or we can use binary tree linked list to apply binary search efficiently.
  2. No, binary search is impossible on a linked list.
  3. Yes, binary search is possible on linked list. It is as efficient as binary search on arrays.
  4. all of the above are true.
Show Answer
Share Facebook gmail Twitter
19
How to implement heterogeneous linked list in c?
  1. It is not possible to create heterogeneous linked list.
  2. create a structure for each node and implement it.
  3. have an array of nodes to create heterogeneous linked list.
  4. Using void pointers.
Show Answer
Share Facebook gmail Twitter
20
How to find a loop in linked list in c?
  1. Have a visited flag in each node of the linked list. Flag it as visited when you reach the node. When you reach a node and the flag is already flagged as visited, then you know there is a loop in the linked list.
  2. Have 2 pointers to start of the linked list. Increment one pointer by 1 node and the other by 2 nodes. If there’s a loop, the 2nd pointer will meet the 1st pointer somewhere. If it does, then you know there’s one.
  3. either option 1 or 2.
  4. Both the options will not work.
Show Answer
Share Facebook gmail Twitter
Read more papers :