0
0
DSA Cprogramming~10 mins

Graph vs Tree Key Structural Difference in DSA C - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a tree node struct with an integer value.

DSA C
typedef struct TreeNode {
    int [1];
    struct TreeNode* left;
    struct TreeNode* right;
} TreeNode;
Drag options to blanks, or click blank then click option'
Adata
Bnode
Cvalue
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' which is confusing as it suggests the whole struct.
Using 'val' which is less descriptive.
2fill in blank
medium

Complete the code to check if a graph has a cycle using DFS.

DSA C
bool hasCycle(int node, bool visited[], bool recStack[]) {
    visited[node] = true;
    recStack[node] = true;
    for (int i = 0; i < graph[node].neighborCount; i++) {
        int neighbor = graph[node].neighbors[i];
        if (!visited[neighbor] && hasCycle(neighbor, visited, recStack))
            return [1];
        else if (recStack[neighbor])
            return true;
    }
    recStack[node] = false;
    return false;
}
Drag options to blanks, or click blank then click option'
Atrue
Bvisited[neighbor]
Cnode
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false instead of true when a cycle is found.
Returning the node index instead of a boolean.
3fill in blank
hard

Fix the error in the code that checks if a tree is a valid binary search tree (BST).

DSA C
bool isValidBST(struct TreeNode* root, int min, int max) {
    if (root == NULL) return true;
    if (root->value [1]= min || root->value [2]= max) return false;
    return isValidBST(root->left, min, root->value) && isValidBST(root->right, root->value, max);
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > instead of <= or >= causing incorrect validation.
Mixing up min and max comparisons.
4fill in blank
hard

Fill both blanks to create a graph adjacency list representation using arrays.

DSA C
typedef struct {
    int [1];
    int [2][MAX_NEIGHBORS];
} GraphNode;
Drag options to blanks, or click blank then click option'
AneighborCount
Bneighbors
Cedges
Dadjacency
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'edges' or 'adjacency' which are less precise here.
Mixing the order of count and array fields.
5fill in blank
hard

Fill all three blanks to implement a function that adds an edge to an undirected graph adjacency list.

DSA C
void addEdge(GraphNode graph[], int u, int v) {
    graph[u].[1][graph[u].[2]] = v;
    graph[u].[2]++;
    graph[v].[1][graph[v].[2]] = u;
    graph[v].[2]++;
}
Drag options to blanks, or click blank then click option'
Aneighbors
BneighborCount
Cedges
Dadjacency
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'edges' or 'adjacency' which are not field names here.
Forgetting to increment neighborCount.