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 right side view function for the given binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function rightSideView(root: TreeNode | null): number[] { const result: number[] = []; if (!root) return result; const queue: (TreeNode | null)[] = [root]; while (queue.length > 0) { const size = queue.length; for (let i = 0; i < size; i++) { const node = queue.shift()!; if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); if (i === size - 1) result.push(node.val); } } return result; } // Tree structure: // 1 // / \ // 2 3 // \ \ // 5 4 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
Think about which nodes are visible when looking from the right side level by level.
✗ Incorrect
The right side view shows the last node at each level when traversing from left to right.
Level 1: Node 1
Level 2: Node 3 (right child of 1)
Level 3: Node 4 (right child of 3)
🧠 Conceptual
intermediate1:30remaining
Understanding the Right Side View Concept
Which statement best describes the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Focus on visibility from the right side, not all nodes or sorting.
✗ Incorrect
The right side view includes only the rightmost node at each depth level, as seen from the right side.
🔧 Debug
advanced2:00remaining
Identify the Bug in Right Side View Implementation
What error will this code produce when run, and why?
DSA Typescript
function rightSideView(root: TreeNode | null): number[] {
const result: number[] = [];
if (!root) return result;
const queue: (TreeNode | null)[] = [root];
while (queue.length > 0) {
const size = queue.length;
for (let i = 0; i <= size; i++) {
const node = queue.shift()!;
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
if (i === size - 1) result.push(node.val);
}
}
return result;
}Attempts:
2 left
💡 Hint
Check the loop condition and how many times queue.shift() is called.
✗ Incorrect
The for loop uses i <= size, which runs one extra time than the queue length.
This causes queue.shift() to return undefined, leading to a runtime error when accessing node.left.
❓ Predict Output
advanced2:00remaining
Output of Right Side View for a Left-Skewed Tree
What is the output of the right side view function for this left-skewed binary tree?
DSA Typescript
const root = new TreeNode(10, new TreeNode(20, new TreeNode(30, new TreeNode(40), null), null), null ); console.log(rightSideView(root));
Attempts:
2 left
💡 Hint
In a left-skewed tree, the right side view shows all nodes because no right children hide them.
✗ Incorrect
Each level has only one node, so the right side view includes all nodes in the path.
🚀 Application
expert1:30remaining
Number of Nodes in Right Side View for a Complete Binary Tree
Given a complete binary tree of height 4 (root at height 1), how many nodes will the right side view contain?
Attempts:
2 left
💡 Hint
The right side view contains one node per level.
✗ Incorrect
A complete binary tree of height 4 has 4 levels.
The right side view shows exactly one node per level, so 4 nodes total.