Oraclecoding (Encrypted)
Oraclecoding (Encrypted)
com/
Coding skills
Question 1
Marks: 1
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Internal nodes have one or two children, called "left" and "right".
A tree rooted at a node having left child A and right child B is a different tree than one rooted at
a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
http://placementkit.blogspot.com/
/ \
"B" "C"
What constructors could InternalNode have according to the specifications for the tree structure
(potentially in addition to others)?
InternalNode()
InternalNode(String)
InternalNode(Node)
struct AVLTree
AVLTree * left;
AVLTree * right;
http://placementkit.blogspot.com/
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
http://placementkit.blogspot.com/
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
}
http://placementkit.blogspot.com/
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
http://placementkit.blogspot.com/
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
http://placementkit.blogspot.com/
Consider that the binary tree specified above id given as input to the following function.
if (p==0)
return 0;
return 1;
else
http://placementkit.blogspot.com/
return numofleaves(p->right)+numofleaves(p->left);
Consider that the binary tree specified above id given as input to the following function.
if (p!=0)
else
return 0;
What would be the output of the following code snippet for the above mentioned binary search
tree?
void func() {
std::queue q;
q.push(root);
q.pop();
if (cur->left) q.push(cur->left);
if (cur->right) q.push(cur->right);
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
A tree rooted at a node having left child A and right child B is a different tree than one
rooted at a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
String value;
boolean isTerminal() {
return true;
return value.compareTo(nd.value);
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
if (nd.isTerminal()) return 1;
// . . .
return right.compareTo(ind.right);
Which of the following set of tests would be both a sufficient and necessary part for the
comment for the method to execute without error and return a correct result? (Assume that the
tests return a correct result from the function before the above code is executed if that is
possible.)
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
http://placementkit.blogspot.com/
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
Under which of the following conditions would InternalNode tree1 be “less than” InternalNode
tree2 for the most “natural” interpretation of “less than”?
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
if (nd.isTerminal()) return 1;
return -1;
Given the specifications for the tree structure which conditions could occur that are not handled
correctly in the above code?
struct AVLTree
AVLTree * left;
AVLTree * right;
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
http://placementkit.blogspot.com/
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
http://placementkit.blogspot.com/
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
}
http://placementkit.blogspot.com/
20,5,15,9,13,2,6,12,14,15,16,17,18,19
Let's give the root node of the resulting tree as input to the following code snippet
if (*p!=0)
else
return 0;
What would be the return value after execution of the above code snippet?
struct AVLTree
{
http://placementkit.blogspot.com/
AVLTree * left;
AVLTree * right;
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
http://placementkit.blogspot.com/
return single_rotation_with_left(k3);
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
{
http://placementkit.blogspot.com/
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
http://placementkit.blogspot.com/
*node = double_rotation_with_right(*node);
20,5,15,9,13,2,6,12,14,15,16,17,18,19
Let's give the root node of the resulting tree as input to the following code snippet
if (*p!=0)
else
return 0;
if (*p!=0)
else
return 0;
What would be the return value after execution of the above code snippet?
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Internal nodes have one or two children, called "left" and "right".
A tree rooted at a node having left child A and right child B is a different tree than one rooted at
a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
http://placementkit.blogspot.com/
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
What constructors should Node have according to the specifications for the tree structure?
Node(Node)
Node(String)
d. A
Question 13
Marks: 1
struct AVLTree
AVLTree * left;
AVLTree * right;
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
http://placementkit.blogspot.com/
return node->height;
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
http://placementkit.blogspot.com/
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
{
http://placementkit.blogspot.com/
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
20,5,15,9,13,2,6,12,14,15,16,17,18,19
Let's give the root node of the resulting tree as input to the following code snippet
if (*p!=0)
else
http://placementkit.blogspot.com/
return 0;
if ((*p!=0)&&((*p)->element != 13))
else
return 0;
What would be the return value after execution of the above code snippet?
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Internal nodes have one or two children, called "left" and "right".
A tree rooted at a node having left child A and right child B is a different tree than one rooted at
a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
InternalNode(node l, node r) {
left = l;
right = r;
}
http://placementkit.blogspot.com/
Which of the constructor’s arguments can be null according the specifications of the tree
structure?
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Internal nodes have one or two children, called "left" and "right".
A tree rooted at a node having left child A and right child B is a different tree than one rooted at
a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
/\
/ \
/ \
+ +
/ /\
http://placementkit.blogspot.com/
/ / \
/ / \
"A" + "D"
/\
/ \
/ \
"B" "C"
TerminalNode()
TerminalNode(Node)
TerminalNode(String)
struct AVLTree
AVLTree * left;
http://placementkit.blogspot.com/
AVLTree * right;
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
{
http://placementkit.blogspot.com/
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
http://placementkit.blogspot.com/
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
{
http://placementkit.blogspot.com/
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
http://placementkit.blogspot.com/
*node = double_rotation_with_right(*node);
if (node == NULL)
return NULL;
return node;
else
return searchmin(node->left);
}
http://placementkit.blogspot.com/
if (node == NULL)
return NULL;
return node;
else
return searchmax(node->right);
while((*node!=NULL)&&((*node)->element != i)){
parent = node;
if((*node)->element >i)
*node = (*node)->left;
else
*node = (*node)->right;
http://placementkit.blogspot.com/
AVLTree * x;
AVLTree *tmp_cell;
(*node) = double_rotation_with_right((*node));
else
(*node) = single_rotation_with_right((*node));
http://placementkit.blogspot.com/
(*node) = double_rotation_with_left((*node));
else
(*node) = single_rotation_with_left((*node));
tmp_cell = searchmin((*node)->right);
http://placementkit.blogspot.com/
(*node)->element = tmp_cell->element;
else
tmp_cell = (*node);
if ((*node)->left == NULL)
(*node) = (*node)->right;
(*node) = (*node)->left;
free(tmp_cell);
tmp_cell = NULL;
return (*node);
if ((*p!=0)&&((*p)->element != 13))
else
return 0;
}
http://placementkit.blogspot.com/
if (*p==0)
return 0;
return 1;
else
Which one of the following would be the output of above code snippet?
struct AVLTree
AVLTree * left;
AVLTree * right;
int element;
http://placementkit.blogspot.com/
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
k1 = k2->left;
http://placementkit.blogspot.com/
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
}
http://placementkit.blogspot.com/
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
http://placementkit.blogspot.com/
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
http://placementkit.blogspot.com/
if (node == NULL)
return NULL;
return node;
else
return searchmin(node->left);
{
http://placementkit.blogspot.com/
if (node == NULL)
return NULL;
return node;
else
return searchmax(node->right);
while((*node!=NULL)&&((*node)->element != i)){
parent = node;
if((*node)->element >i)
*node = (*node)->left;
else
*node = (*node)->right;
}
http://placementkit.blogspot.com/
AVLTree * x;
AVLTree *tmp_cell;
(*node) = double_rotation_with_right((*node));
else
(*node) = single_rotation_with_right((*node));
}
http://placementkit.blogspot.com/
(*node) = double_rotation_with_left((*node));
else
(*node) = single_rotation_with_left((*node));
tmp_cell = searchmin((*node)->right);
(*node)->element = tmp_cell->element;
else
tmp_cell = (*node);
if ((*node)->left == NULL)
(*node) = (*node)->right;
(*node) = (*node)->left;
free(tmp_cell);
tmp_cell = NULL;
return (*node);
if ((*p!=0)&&((*p)->element != 13))
else
return 0;
struct AVLTree
AVLTree * left;
AVLTree * right;
int element;
int height;
};
if(a>=b)
http://placementkit.blogspot.com/
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
}
http://placementkit.blogspot.com/
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
}
http://placementkit.blogspot.com/
else
*node = double_rotation_with_left(*node);
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
Which one of the following would be the root node after inserting 16.
struct AVLTree
AVLTree * left;
AVLTree * right;
int element;
int height;
};
if(a>=b)
return a;
if(a<b)
return b;
if (node == NULL)
return -1;
else
return node->height;
AVLTree *k1;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
{
http://placementkit.blogspot.com/
AVLTree *k1;
k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
k3->left = single_rotation_with_right(k3->left);
return single_rotation_with_left(k3);
k3->right = single_rotation_with_left(k3->right);
return single_rotation_with_right(k3);
if (*node == NULL)
if (*node == NULL)
return;
(*node)->element = value;
(*node)->height = 0;
return;
insert(value, &((*node)->left));
if (height((*node)->left) - height((*node)->right) == 2)
*node = single_rotation_with_left(*node);
else
*node = double_rotation_with_left(*node);
}
http://placementkit.blogspot.com/
insert(value, &((*node)->right));
if (height((*node)->right) - height((*node)->left) == 2)
*node = single_rotation_with_right(*node);
else
*node = double_rotation_with_right(*node);
b. 2
c. None of these
d. 1