DataStructures-Interview-Questions
Sunday, December 2, 2012
Binary Trees VS Hash Tables
For what purpose binary trees are preferred over hash tables?
Arrays and Linked Lists
What are arrays and linked lists. In what situation linked lists are prefered
over arrays.
Binary Tree Question
Write a function which takes a binary tree. Return false if any node other than
the leaf nodes have less than 2 nodes. Write test cases
Palindrome using data structure
Implement a palindrome using any data structure (complexity - logn)
Binary Search Tree .. look inside for more details
Insert a node to a Binary Search Tree and test the function. Take an example and
walk through the code. If you were to balance the tree, what would the coding
logic be?
How to find if linked list is circular or not?
Any answers?
********************************************
Have 2 pointers .. one is slow and the other is fast (fast pointer moves twice whereas slow pointer moves once) .. if the list is circular both the pointers will meet meet.
bool findCircular(Node *head)
{
Node *slower, * faster;
slower = head;
faster = head;
while(true) {
// if the faster pointer encounters a NULL element
if( !faster || !faster->next)
return false;
//if faster pointer ever equals slower or faster's next
//pointer is ever equal to slow then it's a circular list
else if (faster == slower || faster->next == slower)
return true;
else{
// advance the pointers
slower = slower->next;
faster = faster->next->next;
}
}
}
********************************************
Have 2 pointers .. one is slow and the other is fast (fast pointer moves twice whereas slow pointer moves once) .. if the list is circular both the pointers will meet meet.
bool findCircular(Node *head)
{
Node *slower, * faster;
slower = head;
faster = head;
while(true) {
// if the faster pointer encounters a NULL element
if( !faster || !faster->next)
return false;
//if faster pointer ever equals slower or faster's next
//pointer is ever equal to slow then it's a circular list
else if (faster == slower || faster->next == slower)
return true;
else{
// advance the pointers
slower = slower->next;
faster = faster->next->next;
}
}
}
Subscribe to:
Posts (Atom)