D. [[0, 1, 0], [0, 1, 0], [0, 1, 0]]; Rows are shallow copies
Solution
Step 1: Understand matrix initialization
Using [[0]*3]*3 creates 3 references to the same inner list.
Step 2: Modify one element affects all rows
Setting matrix[0][1] = 1 changes the second element in all rows because they share the same list.
Final Answer:
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]; Rows are shallow copies -> Option D
Quick Check:
Shallow copy rows cause all rows to change [OK]
Hint: Avoid [[0]*n]*n; use nested list comprehension [OK]
Common Mistakes:
Assuming only first row changes
Ignoring shallow copy behavior
Confusing IndexError with shallow copy bug
5. You have a graph with 1000 vertices and about 10,000 edges. You need to frequently check if an edge exists between two vertices. Which representation is best and why?
hard
A. Edge List, because it is simplest to implement
B. Adjacency Matrix, because it allows O(1) edge existence checks
C. Adjacency List, because it uses less memory for sparse graphs
D. Incidence Matrix, because it stores edges explicitly
Solution
Step 1: Analyze graph density and operations
Graph has 1000 vertices and 10,000 edges, which is moderately sparse but not very sparse.
Step 2: Consider operation speed for edge checks
Adjacency matrix allows checking edge existence in constant time O(1), while adjacency list requires searching neighbors.
Step 3: Balance memory and speed
Though adjacency matrix uses more memory, the speed benefit for frequent edge checks outweighs memory cost here.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option B
Quick Check:
Frequent edge check = Adjacency Matrix [OK]
Hint: For fast edge checks, adjacency matrix is best despite memory [OK]
Common Mistakes:
Choosing adjacency list only for memory without considering speed
Ignoring operation frequency in choice
Confusing edge list with adjacency matrix for fast checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.