Professional Documents
Culture Documents
Q1- Given an array of positive and negative numbers, arrange them in an alternate fashion such that
every positive number is followed by negative and vice versa maintaining the order of appearance.If the
count of negative numbers is more keep the extra at last in array
constraint : Space complexity should be O(1).
Q2- Given an array of random numbers, Push all the zeros of a given array to the right end of the array in
minimum possible swaps. Order of appearance doesnt matter. Print the total nonzero numbers and
minimum swaps needed to do so.
input : {1, 9, 8, 0, 0, -2, 0, 1, 6}.
output :
nonzero : 6
swaps : 2 (-2 as it is and swap 1 and 6 from first two zeros. )
18 were selected out of 55 for f2f round.
Round 1 F2F :
Q1- Two linked lists merge at one point, return the converging node. Constraint- O(1) space and O(m+n)
,where m and n are lengths of lists.
Link: http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/
Q2- Rotate the alternate levels of a binary tree.
Input:
1
/
2
/
\
5
4
/
3
/
\
7
/ \
12 13
/ \
11 10
8
Output:
1
/
3
/
2
\
7
6
/
13
/
4
\
\
5
\ /
\
12 8 11 10
First he asked to do it without recursion and then with recursion. O(n) time complexity.
Q3 Write an efficient function that takes two strings as arguments and removes the second string from
first string (in place). (Shifting not allowed)
input:
str1: aabcabcb
str2: abc
output: ab
Q4 Insert an element into a sorted link list which is having loop somewhere and duplicate elements as
well.
Q5 Make your own data structure. which inserts, deletes and gives a random number in O(1) time.
Hint : Use hash table and array.
Round 2 F2F :
Q1 You have n pencils, each having l length. Each can write 4 kilometers. After writing 4 kilometers it
has l/4 length. Then you can join 4 pencils which are having l/4 length and can make 1 pencil. You cant
make pencil of pieces if remaining pieces are 3 or 2 or 1 in number. And you can include these remaining
pieces whenever you need. Write a recursive relation independent of l,length of given pencil, for how
much one can write from n pencils. Write mathematical equation also.
Q2 Find
Input:
1
/
2
/
4
/
8
3
\
5
6
/ \
11 10
\
7
/ \
12 13
output:
13 12 11 10 8 7 6 5 4 3 2 1
You are permitted to use extra space and now print them in separate levels too.
Output:
13 12 11 10 8
7654
32
1
last->next = *head_ref;
/* Change the head pointer to point to last node now */
*head_ref = last;
}
/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref)
= new_node;
}
printList(start);
getchar();
}
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
/* 2. allocate new node */
struct node* new_node =(struct node*) malloc(sizeof(struct node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
Five of the above steps step process are same as the 5 steps used for inserting after a given node in
singly linked list. The two extra steps are needed to change previous pointer of new node and previous
pointer of new nodes next node.
3) Add a node at the end: (7 steps process)
The new node is always added after the last node of the given Linked List. For example if the given DLL
is 5<->10<->15<->20<->25 and we add an item 30 at the end, then the DLL becomes 5<->10<->15<>20<->25<->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then
change the next of last node to new node.
Following are the 7 steps to add node at the end.
/* Given a reference (pointer to pointer) to the head
of a DLL and an int, appends a new node at the end */
void append(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct
node));
struct node *last = *head_ref;
*/
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;