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 Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func rightSideView(root *TreeNode) []int { if root == nil { return []int{} } var result []int queue := []*TreeNode{root} for len(queue) > 0 { size := len(queue) for i := 0; i < size; i++ { node := queue[0] queue = queue[1:] if i == size-1 { result = append(result, node.Val) } if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } } return result } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Right = &TreeNode{Val: 3} root.Left.Right = &TreeNode{Val: 5} root.Right.Right = &TreeNode{Val: 4} fmt.Println(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 traversed from left to right. At level 1: 1, level 2: 3, level 3: 4.
🧠 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 what nodes are visible from the right side perspective.
✗ Incorrect
The right side view includes only the nodes that are visible when looking at the tree from the right side, which means the rightmost node at each level.
🔧 Debug
advanced2:00remaining
Identify the Bug in Right Side View Implementation
What error will occur when running this code snippet for right side view of a binary tree?
DSA Go
func rightSideView(root *TreeNode) []int { if root == nil { return nil } var result []int queue := []*TreeNode{root} for len(queue) > 0 { size := len(queue) for i := 0; i < size; i++ { node := queue[0] queue = queue[1:] if i == size-1 { result = append(result, node.Val) } if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } } return result }
Attempts:
2 left
💡 Hint
Check the loop condition carefully for off-by-one errors.
✗ Incorrect
The for loop uses i <= size which causes one extra iteration leading to accessing queue[0] when queue is empty, causing index out of range error.
🚀 Application
advanced1:30remaining
Right Side View for a Skewed Binary Tree
Given a binary tree where every node has only a left child, what will be the right side view output?
DSA Go
root := &TreeNode{Val: 1}
root.Left = &TreeNode{Val: 2}
root.Left.Left = &TreeNode{Val: 3}
root.Left.Left.Left = &TreeNode{Val: 4}
// Call rightSideView(root)Attempts:
2 left
💡 Hint
Think about which nodes are visible from the right side when the tree is skewed left.
✗ Incorrect
Since there are no right children, the right side view will show all nodes as they appear from the right side, which is the same as the left skewed path.
❓ Predict Output
expert2:30remaining
Output of Right Side View for Complex Tree with Multiple Levels
What is the output of the rightSideView function for the following tree structure?
DSA Go
root := &TreeNode{Val: 10}
root.Left = &TreeNode{Val: 5}
root.Right = &TreeNode{Val: 15}
root.Left.Left = &TreeNode{Val: 3}
root.Left.Right = &TreeNode{Val: 7}
root.Right.Left = &TreeNode{Val: 12}
root.Right.Right = &TreeNode{Val: 18}
root.Left.Right.Right = &TreeNode{Val: 8}
root.Right.Right.Left = &TreeNode{Val: 17}
// Call rightSideView(root)Attempts:
2 left
💡 Hint
Remember to pick the rightmost node at each level.
✗ Incorrect
Level 1 rightmost: 10, level 2 rightmost: 15, level 3 rightmost: 18, level 4 rightmost: 17 (since 18 has left child 17).