Challenge - 5 Problems
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Right Side View for a Simple Binary Tree
What is the output of the following JavaScript code that prints the right side view of a binary tree?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function rightSideView(root) { const result = []; if (!root) return result; const queue = [root]; while (queue.length) { const levelSize = queue.length; for (let i = 0; i < levelSize; i++) { const node = queue.shift(); if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); if (i === levelSize - 1) result.push(node.val); } } return result; } const root = new TreeNode(1, new TreeNode(2, null, new TreeNode(5)), new TreeNode(3, null, new TreeNode(4))); console.log(rightSideView(root));
Attempts:
2 left
💡 Hint
Remember, the right side view shows the last node at each level when viewed from the right side.
✗ Incorrect
The right side view collects the last node at each level. Level 0 has 1, level 1 has 3, and level 2 has 4 as the rightmost nodes.
🧠 Conceptual
intermediate1:30remaining
Understanding the Right Side View Concept
Which of the following best describes the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Think about which nodes you see if you look at the tree from the right side.
✗ Incorrect
The right side view includes the last node at each level when the tree is viewed from the right side.
❓ Predict Output
advanced2:00remaining
Output of Right Side View with Unbalanced Tree
What is the output of the following code that prints the right side view of an unbalanced binary tree?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function rightSideView(root) { const result = []; if (!root) return result; const queue = [root]; while (queue.length) { const levelSize = queue.length; for (let i = 0; i < levelSize; i++) { const node = queue.shift(); if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); if (i === levelSize - 1) result.push(node.val); } } return result; } const root = new TreeNode(10, new TreeNode(5, new TreeNode(3), null), new TreeNode(15, null, new TreeNode(20))); console.log(rightSideView(root));
Attempts:
2 left
💡 Hint
Check the last node at each level from left to right in the queue.
✗ Incorrect
Level 0 rightmost is 10, level 1 rightmost is 15, level 2 rightmost is 20.
🔧 Debug
advanced1:30remaining
Identify the Error in Right Side View Implementation
What error will the following code produce when trying to get the right side view of a binary tree?
DSA Javascript
function rightSideView(root) {
const result = [];
if (!root) return result;
const queue = [root];
while (queue.length) {
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
if (i === levelSize - 1) result.push(node.val);
}
}
return result;
}Attempts:
2 left
💡 Hint
Look carefully at the for loop condition and how queue.shift() behaves.
✗ Incorrect
The for loop runs one extra time (i <= levelSize), causing queue.shift() to return undefined, then accessing node.left causes a TypeError.
🧠 Conceptual
expert1:30remaining
Time Complexity of Right Side View Algorithm
What is the time complexity of the standard breadth-first search approach to find the right side view of a binary tree with n nodes?
Attempts:
2 left
💡 Hint
Consider how many times each node is processed in the queue.
✗ Incorrect
The algorithm visits each node exactly once in a breadth-first manner, so the time complexity is O(n).