Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Technical & Managerial Round:

1).How will you get to know the size of int without using sizeof()?
Sol: int *ptr = 0;
   Ptr++;
 printf("Size of int data type:  %d",ptr);

2). The logic to reverse a linked list?

i)Iterate a LL and store all the elements and construct a new LL . Time : o(n), space :
o(2n)

ii)Using recursion – simple approach


Node* reverse(Node* node) 
    { 
        if (node == NULL) 
            return NULL; 
        if (node->next == NULL) { 
            head = node; 
            return node; 
        } 
        Node* node1 = reverse(node->next); 
        node1->next = node; 
        node->next = NULL; 
        return node; 
    } 

iii)Recursion
void reverse(Node **head) 

    if (!head) 
        return; 
    reverseUtil(*head, NULL, head); 

void reverseUtil(Node *curr, Node *prev, Node **head) 

    /* If last node mark it head*/
    if (!curr->next) 
    { 
        *head = curr; 
  
        /* Update next to prev node */
        curr->next = prev; 
        return;     } 
  
    /* Save curr->next node for recursive call */
    Node *next = curr->next; 
  
    /* and update next ..*/
    curr->next = prev; 
  
    reverseUtil(next, curr, head); } 
iv) Time Complexity: O(n)
Space Complexity: O(1)

void recursiveReverse(struct Node** head_ref) 



    struct Node* first; 
    struct Node* rest; 
       
    /* empty list */
    if (*head_ref == NULL) 
       return;    
  
    /* suppose first = {1, 2, 3}, rest = {2, 3} */
    first = *head_ref;   
    rest  = first->next; 
  
    /* List has only one node */
    if (rest == NULL) 
       return;    
  
    /* reverse the rest list and put the first element at the end */
    recursiveReverse(&rest); 
    first->next->next  = first;   
      
    /* tricky step -- see the diagram */
    first->next  = NULL;           
  
    /* fix the head pointer */
    *head_ref = rest;               

v) Iteratively Reverse a linked list using only 2 pointers (An Interesting Method) Time
Complexity: O(n)

//The idea is to use XOR to swap pointers. (Where??)


def reverse(self):
        prev = None
        current = self.head
        while current is not None:
            next, current.next = current.next, prev
            prev, current = current, next
        self.head = prev
vi) Time Complexity: O(n)
Space Complexity: O(1)
def reverse(self): 
        prev = None
        current = self.head 
        while(current is not None): 
            next = current.next
            current.next = prev 
            prev = current 
            current = next
        self.head = prev
3)Explain the terms Pre-order, In-Order and Post -order with examples (Trees)

4)Check whether the given linked list is either NULL-terminated or ends in a cycle or Detect
loop in linked list with O(n^2) and O(n)?

Brute-Force Approach:
As an example, consider the following linked list which has a loop in it. The difference
between this list and the regular list is that, in this list, there are two nodes whose next
pointers are the same. In regular singly linked lists (without a loop) each node’s next pointer
is unique. That means the repetition of next pointers indicates the existence of a loop

Sorting technique:
Consider the following algorithm which is based on sorting. 
Algorithm:
 • Traverse the linked list nodes one by one and take all the next pointer values into an array.
• Sort the array that has the next node pointers. 
• If there is a loop in the linked list, definitely two next node pointers will be pointing to the
same node. 
• After sorting if there is a loop in the list, the nodes whose next pointers are the same will
end up adjacent in the sorted list.
• If any such pair exists in the sorted list then we say the linked list has a loop in it. 
Time Complexity=O(nlogn) for sorting the next pointers array. 
Space Complexity=O(n) for the next pointers array. 
Problem with the above algorithm: The above algorithm works only if we can find the length
of the list. 
But if the list has a loop then we may end up in an infinite loop. Due to this reason the
algorithm fails.

Can we solve the Problem in O(n)?

Yes. Efficient Approach (Memoryless Approach): This problem was solved by Floyd.
The solution is named the Floyd cycle finding algorithm. It uses two pointers moving at
different speeds to walk the linked list. Once they enter the loop they are expected to meet,
which denotes that there is a loop. This works because the only way a faster moving pointer
would point to the same location as a slower moving pointer is if somehow the entire list or a
part of it is circular. Think of a tortoise and a hare running on a track. The faster running hare
will catch up with the tortoise if they are running in a loop. As an example, consider the
following example and trace out the Floyd algorithm. From the diagrams below we can see
that after the final step they are meeting at some point in the loop which may not be the
starting point of the loop. Note: slowPtr (tortoise) moves one pointer at a time and fastPtr
(hare) moves two pointers at a time.

5) Why quicksort is faster compared to Merge sort? 


Ans : Mergesort uses extra space, quicksort requires little space and exhibits good
cache locality. [Reason why ubuntu uses quick sort over merge sort]

6) Which sorting algorithm is efficient when array size is small?


Ans : Insertion sort or selection sort are both typically faster for small arrays
7) Which DS has less search, insert, delete time complexity?
Ans : A linked list provides efficient insertion and deletion of arbitrary elements.
Deletion here is deletion by iterator, not by value. Traversal is quite fast. A dequeue
provides efficient insertion and deletion only at the ends, but those are faster than for
a linked list, and traversal is faster as well.

8)  How to implement stack using two queues.

9) How do you delete a node in linked list when head pointer is not given and only
address of the node that needs to be deleted is given? (Note: The linked list will have
minimum two nodes and the deleted node address will not be of last node)

10) complexities of different sorting algos

11) Questions based on Resume

You might also like