In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
AO(V + E)
BO(V²)
CO(E²)
DO(V * E)
Correct Answer:
A. O(V + E)
EXPLANATION
Using dynamic adjacency lists, you allocate V list pointers and E edge nodes total, giving O(V + E) space complexity, which is optimal for sparse graphs.
What potential issue occurs with this code in a large-scale system?
while(1) { int *arr = malloc(1000000); }
AMemory exhaustion - repeated allocation without deallocation
BStack overflow
CCompilation error
DSegmentation fault immediately
Correct Answer:
A. Memory exhaustion - repeated allocation without deallocation
EXPLANATION
This creates an infinite loop that allocates 1MB each iteration without freeing, rapidly exhausting available memory. This is a classic memory leak scenario.
In the 2024-2025 competitive exam pattern, a dynamic array needs to grow from 100 to 200 elements. Using realloc(), what should be the concern?
AOld pointers to the array may become invalid after realloc()
Brealloc() always allocates new memory
CData is lost during realloc()
Drealloc() is slower than malloc() + memcpy()
Correct Answer:
A. Old pointers to the array may become invalid after realloc()
EXPLANATION
If realloc() needs to relocate the memory block to a different address, all old pointers to the array become invalid. Only the returned pointer should be used.
Correct Answer:
A. int *p = malloc(100); p = malloc(50);
EXPLANATION
In option A, the first malloc(100) block becomes inaccessible after p is reassigned, but it's not freed, causing a memory leak. The original 100 bytes are lost.
Correct Answer:
A. Buffer overflow - accessing beyond allocated memory
EXPLANATION
malloc(10) allocates 10 bytes, but accessing ptr[11] goes beyond the allocated boundary, causing a buffer overflow which leads to undefined behavior and potential segmentation fault.
In a competitive programming problem, you need to allocate a 2D array of size 5x5 dynamically. Which approach is most efficient?
AAllocate single block of 25 integers and use 2D indexing
BAllocate 5 pointers, then allocate 5 integers for each pointer
CUse stack allocation with fixed size
DUse global arrays instead of dynamic allocation
Correct Answer:
A. Allocate single block of 25 integers and use 2D indexing
EXPLANATION
Allocating a single contiguous block is more cache-efficient and requires only one malloc() call, making it better for competitive programming where memory access speed matters.
What happens when malloc() fails to allocate memory?
AIt returns NULL
BIt throws an exception
CIt allocates memory from stack
DIt terminates the program
Correct Answer:
A. It returns NULL
EXPLANATION
When malloc() cannot allocate the requested memory, it returns NULL. The programmer must check for NULL before using the pointer to avoid undefined behavior.