Challenge - 5 Problems
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Width Calculation
What is the output of the following code that calculates the maximum width of a binary tree?
DSA C
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// Queue node for BFS
typedef struct QueueNode {
struct TreeNode* node;
unsigned long long index;
struct QueueNode* next;
} QueueNode;
// Queue structure
typedef struct Queue {
QueueNode* front;
QueueNode* rear;
} Queue;
void enqueue(Queue* q, TreeNode* node, unsigned long long index) {
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
newNode->node = node;
newNode->index = index;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
}
void dequeue(Queue* q) {
if (q->front == NULL) return;
QueueNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
free(temp);
}
int isEmpty(Queue* q) {
return q->front == NULL;
}
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
unsigned long long maxWidth = 0;
Queue q = {NULL, NULL};
enqueue(&q, root, 0ULL);
while (!isEmpty(&q)) {
int size = 0;
QueueNode* temp = q.front;
while (temp) {
size++;
temp = temp->next;
}
unsigned long long start = q.front->index;
unsigned long long end = q.rear->index;
if (end - start + 1 > maxWidth) maxWidth = end - start + 1;
for (int i = 0; i < size; i++) {
TreeNode* node = q.front->node;
unsigned long long idx = q.front->index;
dequeue(&q);
if (node->left) enqueue(&q, node->left, 2 * (idx - start));
if (node->right) enqueue(&q, node->right, 2 * (idx - start) + 1);
}
}
return (int)maxWidth;
}
int main() {
TreeNode n1 = {1, NULL, NULL};
TreeNode n2 = {3, NULL, NULL};
TreeNode n3 = {2, NULL, NULL};
TreeNode n4 = {5, NULL, NULL};
TreeNode n5 = {3, NULL, NULL};
TreeNode n6 = {9, NULL, NULL};
n1.left = &n2;
n1.right = &n3;
n2.left = &n4;
n2.right = &n5;
n3.right = &n6;
int result = widthOfBinaryTree(&n1);
printf("%d\n", result);
return 0;
}Attempts:
2 left
💡 Hint
Think about the maximum number of nodes between the leftmost and rightmost nodes at any level.
✗ Incorrect
The maximum width is the largest number of nodes between the leftmost and rightmost nodes at any level, counting nulls in between. Here, the maximum width is 4 at the third level.
🧠 Conceptual
intermediateUnderstanding Indexing in Maximum Width Calculation
Why do we subtract the start index from each node's index before enqueueing children in the maximum width calculation?
Attempts:
2 left
💡 Hint
Consider what happens to indices as the tree depth increases.
✗ Incorrect
Subtracting the start index keeps the indices relative and small, preventing overflow when the tree is very deep.
🔧 Debug
advancedIdentify the Error in Width Calculation
What error will the following code produce when calculating the maximum width of a binary tree?
DSA C
int widthOfBinaryTree(TreeNode* root) { if (!root) return 0; unsigned long long maxWidth = 0; Queue q = {NULL, NULL}; enqueue(&q, root, 0ULL); while (!isEmpty(&q)) { int size = 0; QueueNode* temp = q.front; while (temp) { size++; temp = temp->next; } unsigned long long start = q.front->index; unsigned long long end = q.rear->index; if (end - start + 1 > maxWidth) maxWidth = end - start + 1; for (int i = 0; i < size; i++) { // Note the <= here changed to < TreeNode* node = q.front->node; unsigned long long idx = q.front->index; dequeue(&q); if (node->left) enqueue(&q, node->left, 2 * (idx - start)); if (node->right) enqueue(&q, node->right, 2 * (idx - start) + 1); } } return (int)maxWidth; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The for loop uses <= size, causing one extra dequeue call when the queue is empty, leading to a segmentation fault.
📝 Syntax
advancedSyntax Error in Queue Implementation
Which option contains a syntax error in the queue enqueue function?
DSA C
void enqueue(Queue* q, TreeNode* node, unsigned long long index) {
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
newNode->node = node;
newNode->index = index;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
}Attempts:
2 left
💡 Hint
Look carefully at the lines inside the else block.
✗ Incorrect
The line 'q->rear->next = newNode' is missing a semicolon, causing a syntax error.
🚀 Application
expertMaximum Width with Sparse Tree
Given a binary tree where only the leftmost and rightmost nodes exist at the last level, what is the maximum width returned by the algorithm?
DSA C
/* Tree structure: 1 / \ 2 3 / \ 4 5 */ TreeNode n1 = {1, NULL, NULL}; TreeNode n2 = {2, NULL, NULL}; TreeNode n3 = {3, NULL, NULL}; TreeNode n4 = {4, NULL, NULL}; TreeNode n5 = {5, NULL, NULL}; n1.left = &n2; n1.right = &n3; n2.left = &n4; n3.right = &n5; int result = widthOfBinaryTree(&n1); printf("%d\n", result);
Attempts:
2 left
💡 Hint
Remember the algorithm counts nulls between leftmost and rightmost nodes at each level.
✗ Incorrect
At the last level, nodes 4 and 5 are at positions 0 and 3 respectively, so width = 3 - 0 + 1 = 4.
