In a dynamic stack implementation for competitive programming, how should you handle reallocation when the stack becomes full?
Answer: A
Doubling capacity and using realloc() provides amortized O(1) insertion with manageable memory overhead, which is the standard approach for dynamic data structures.
Q.862Hard
Which of these represents proper error handling for malloc in competitive programming?
Answer: A
Checking if malloc returns NULL before using the pointer is essential error handling. If allocation fails, the program should handle it gracefully rather than proceeding with NULL.
Q.863Easy
How many bytes are allocated by malloc(sizeof(int) * 10) on a system where int is 4 bytes?
Which of the following correctly deallocates a 2D dynamically allocated array created as int **arr?
Answer: B
A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.
Q.865Easy
What is the primary difference between calloc() and malloc() in C?
Answer: B
calloc(n, size) allocates n*size bytes and initializes all to 0, while malloc(size) allocates size bytes with garbage values.
Advertisement
Q.866Easy
In a dynamic queue implementation, if front pointer is NULL, what does it indicate?
Answer: C
In queue implementations using linked lists, NULL front pointer indicates queue is empty as there are no nodes to dequeue.
Q.867Medium
What happens when realloc() is called on a pointer allocated by malloc() with a larger size?
Answer: C
realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.
Q.868Medium
For implementing a dynamic circular buffer of size n, how many pointers are minimally required?
Answer: B
A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.
Q.869Medium
Which scenario is most likely to cause a segmentation fault in dynamic memory?
Answer: B
Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.
Q.870Medium
In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
Answer: A
Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.
Q.871Medium
What is the correct way to store variable-length strings dynamically?
Answer: B
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.
Q.872Easy
If malloc() returns NULL, what should a program do?
Answer: B
NULL return from malloc indicates allocation failure; ignoring causes dereferencing NULL which leads to crash.
Q.873Easy
In implementing a dynamic doubly-linked list, what additional field is required compared to singly-linked list?
Answer: B
Doubly-linked list nodes require both next and previous pointers to enable bidirectional traversal unlike singly-linked lists.
Q.874Hard
For a dynamic hash table with chaining, if collision occurs, the new element should be inserted where?
Answer: D
While both work, inserting at beginning provides O(1) insertion; end requires traversal. Choice depends on use case.
Q.875Hard
What is the memory overhead when allocating 1000 integers vs 1 integer in a system with malloc metadata?
Answer: C
Each malloc() call has fixed metadata overhead (typically 8-16 bytes). Single allocation of 1000 ints has less overhead than 1000 separate allocations.
Q.876Hard
In competitive coding, when implementing dynamic trees, which traversal is most suitable for level-order?
Answer: C
Level-order traversal requires BFS which uses a queue. Dynamic queue allocation handles arbitrary tree sizes efficiently.
Q.877Hard
Which memory allocation technique provides better locality of reference for accessing array elements?
Answer: B
Contiguous allocation places array elements next to each other in memory, improving cache hits and CPU performance through spatial locality.
Q.878Easy
Which of the following is NOT a preprocessor directive in C?
Answer: D
#declare is not a valid preprocessor directive. Valid directives include #define, #include, #pragma, #ifdef, #endif, #if, #else, etc.
Q.879Easy
What is the output of the following code?
#define MAX 5
int arr[MAX];
Answer: B
The preprocessor replaces MAX with 5, creating an array of size 5. This is a valid use of macro expansion.
Q.880Easy
Which preprocessor directive is used to include standard library files?
Answer: C
#include <filename> is used for standard library files (searched in standard paths). #include "filename" is used for user-defined header files (searched in current directory first).