Sei sulla pagina 1di 12

Tree Interview Question: How would you check if a binary tree is balanced?

Write a C/C++ program

A tree is considered balanced when the difference between the min depth and max depth does not exceed 1. Recursive algorithms always work well on trees, so heres some code. int min_depth( Node * root ) { if( !root ) { return 0; } return 1 + min( min_depth( root->left ), min_depth( root->right )); } int max_depth( Node * root ) { if( !root ) { return 0; } return 1 + max( max_depth( root->left ), max_depth( root->right )); } bool is_balanced( Node * root ) { return ( max_depth( root ) - min_depth( root ) ) <= 1 }

Tree Interview Question: Write a C program to delete a tree (i.e, free up its nodes)

Solutions: clear(struct node* pNode) { if (pNode != NULL) { clear(pNode->left); clear(pNode->right); delete pNode;

} }

Tree Interview Question: Write a C program to determine the number of elements (or size) in a tree. int tree_size(struct node* node) { if (node==NULL) { return(0); } else { return(tree_size(node->left) + tree_size(node->right) + 1); } }

Tree Interview Question: Write a C program to find the depth or height of a tree.

tree_height(mynode *p) { if(p==NULL)return(0); if(p->left){h1=tree_height(p->left);} if(p=>right){h2=tree_height(p->right);} return(max(h1,h2)+1); }

The degree of the leaf is zero. The degree of a tree is the max of its element degrees. A binary tree of height n, h > 0, has at least h and at most (2^h -1) elements in it. The height of a binary tree that contains n, n>0, elements is at most n and atleast log(n+1) to the base 2. Log(n+1) to the base 2 = h n = (2^h - 1)

C/C++ Data Structures Interview Question:How to create a copy of a linked list? Write a C program to create a copy of a linked list.

copy_linked_lists(struct node *q, struct node **s) { if(q!=NULL) { *s=malloc(sizeof(struct node)); (*s)->data=q->data; (*s)->link=NULL; copy_linked_list(q->link, &((*s)->link)); }}

C/C++ Data Structures Interview Question:How to compare two linked lists? Write a C program to compare two linked lists. int compare_linked_lists(struct node *q, struct node *r) { static int flag; if((q==NULL ) && (r==NULL)) { flag=1; } else { if(q==NULL || r==NULL) { flag=0; } if(q->data!=r->data) { flag=0; } else { compare_linked_lists(q->link,r->link); } } return(flag); }

C/C++ Data Structures Interview Question:If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link, pointer, to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.

C/C++ Data Structures Interview Question:How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.

typedef struct node { void *data; struct node *next; }mynode; mynode * find_loop(NODE * head) { mynode *current = head; while(current->next != NULL) { mynode *temp = head; while(temp->next != NULL && temp != current) { if(current->next == temp) { printf("\nFound a loop."); return current; } temp = temp->next; } current = current->next; } return NULL;

C/C++ Data Structures Interview Question:How do you find the middle of a linked list? Write a C program to return the middle of a linked list

#include<stdio.h> #include<ctype.h> typedef struct node { int value; struct node *next; struct node *prev; }mynode;

void add_node(struct node **head, int value); void print_list(char *listName, struct node *head); void getTheMiddle(mynode *head);

int main() { mynode *head; head = (struct node *)NULL;

add_node(&head, 1); add_node(&head, 10); add_node(&head, 5); add_node(&head, 70); add_node(&head, 9); add_node(&head, -99); add_node(&head, 0); add_node(&head, 555); add_node(&head, 55); print_list("myList", head); getTheMiddle(head); getch(); return(0); }

void getTheMiddle(mynode *head) { mynode *p = head; mynode *q = head; if(q!=NULL) { while((q->next)!=NULL && (q->next->next)!=NULL) {

p=(p!=(mynode *)NULL?p->next:(mynode *)NULL); q=(q!=(mynode *)NULL?q->next:(mynode *)NULL); q=(q!=(mynode *)NULL?q->next:(mynode *)NULL); } printf("The middle element is [%d]",p->value); } }

void add_node(struct node **head, int value) { mynode *temp, *cur; temp = (mynode *)malloc(sizeof(mynode)); temp->next=NULL; temp->prev=NULL; if(*head == NULL) { *head=temp; temp->value=value; } else { for(cur=*head;cur->next!=NULL;cur=cur->next); cur->next=temp;

temp->prev=cur; temp->value=value; } }

void print_list(char *listName, struct node *head) { mynode *temp; printf("\n[%s] -> ", listName); for(temp=head;temp!=NULL;temp=temp->next) { printf("[%d]->",temp->value); } printf("NULL\n"); }

C/C++ Data Structures Interview Question:How do you reverse a linked list without using any C pointers?

C/C++ Data StructuresInterview Question:How to declare a structure of a linked list?

Data Structures: Generic Linked List: Write a C program to implement a Generic Linked List....

C/C++ Data Structures: Breadth First Search: Write code for doing a breadth first search in a Tree data structure...

C/C++ Data Structures: Implement a Queue using an Array. Make efficient use of the space in the array.

Linked List: Loop/Cycle Detection: Given the head pointer to a singly linked list with a loop or cycle in it...

Linked List: Merged List Problem: There are 2 singly linked lists (list1 and list2)...

Linked List:Find the middle element: Given a singly linked list, find the node in the middle...

Linked List: Find n-th element from the tail: Given a singly linked list find the nth node from the back...

C/C++ Data Structures: What are the advantages and disadvantages of B-star trees over Binary trees...

Which data strucutres algorithm used in solving the eight Queens problem? Backtracking

In an AVL tree, at what condition the balancing is to be done? If the "pivotal value", or the "height factor", is greater than one or less than minus one.

. What is the bucket size, when the overlapping and collision occur at the same time? The answer is one. If there is only one entry possible in the bucket, when the collision occurs, there is no way to accommodate the colliding value. This results in the overlapping of values.

There are 8, 15, 13, and 14 nodes in 4 different trees. Which one of them can form a full binary tree? The answer is the tree with 15 nodes. In general, there are 2^n-1 nodes in a full binary tree. By the method of elimination: Full binary trees contain odd number of nodes, so there cannot be full binary trees with 8 or 14 nodes. Moreover, with 13 nodes you can form a complete binary tree but not a full binary tree. Thus, the correct answer is 15.

There are four people who want to cross a bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk together at the rate of the slower persons pace. For example, if person 1 and person 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If person 4 returns the flashlight, a total of 20 minutes have passed and you have failed the mission.

Let 1, 2, 5, 10 be labels representing the men of the problem, f represent the flashlights location, and the number in the parenthesis be the total amount of time elapsed. The following sequence of moves solves the problem:

What is the minimum number of queues needed to implement the priority queue?

Please enable JavaScript to view this page content properly. Two. One queue is used for the actual storing of data, and the other one is used for storing the priorities.

Which data structure is used to perform recursion?

Please enable JavaScript to view this page content properly. The answer is Stack. Stack has the LIFO (Last In First Out) property; it remembers it's caller. Therefore, it knows to whom it should return when the function has to return. On the other hand, recursion makes use of the system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written explicit, stack is to be used.

What pointer type is used to implement the heterogeneous linked list in C?

Please enable JavaScript to view this page content properly. The answer is the void pointer. The heterogeneous linked list contains different data types in it's nodes and we need a link, pointer, to connect them. Since we can't use ordinary pointers for this, we use the void pointer. Void pointer is a generic pointer type, and capable of storing pointer to any type.

What are some of the applications for the tree data structure?

Please enable JavaScript to view this page content properly. arithmetic expressions. 2- Symbol table construction. 3- Syntax analysis.

1- Manipulation of the

Potrebbero piacerti anche