Data Structures Fundamentals: 329 MCQs with answers
Showing 200 of 329 multiple-choice questions from this kit.
Warm-up · 111 questions
Q1. When is memory allocated for a static data structure?
BeginnerShow answer & explanation
At compile time
Static data structures allocate memory at compile time, meaning the size is fixed before the program runs.
Q2. Which of the following is an example of a dynamic data structure?
BeginnerShow answer & explanation
Linked list
A linked list is dynamic because nodes are created at runtime and can be added or removed.
Q3. What is the main advantage of static data structures?
BeginnerShow answer & explanation
Faster access due to contiguous memory
Static structures use contiguous memory, enabling direct indexing and faster access.
Q4. What does 'static' refer to in a static data structure?
BeginnerShow answer & explanation
The size is fixed
Static refers to the fixed size, not immutability of values. You can modify elements but not the number of elements.
Q5. Which of the following is a drawback of dynamic data structures?
BeginnerShow answer & explanation
They are slower to access due to pointer traversal
Dynamic structures often use non-contiguous memory, so accessing elements requires following pointers, which is slower.
Q6. In C, which function is used to deallocate dynamic memory?
BeginnerShow answer & explanation
free()
free() is used to release memory that was previously allocated with malloc() or calloc().
Q7. Which scenario is best suited for a static data structure?
BeginnerShow answer & explanation
Storing the 12 months of a year
When the data size is known and fixed, like 12 months, a static array is efficient and simple.
Q8. What is a memory leak?
BeginnerShow answer & explanation
Memory that is allocated but never freed
A memory leak occurs when dynamically allocated memory is not freed, gradually exhausting available memory.
Q9. Which of the following is true about dynamic arrays (e.g., using realloc)?
BeginnerShow answer & explanation
They can be contiguous while still being dynamic in size
Dynamic arrays use contiguous memory but can resize at runtime using realloc, so they combine contiguity with flexibility.
Q10. Why do most real-world applications prefer dynamic structures?
BeginnerShow answer & explanation
They can handle variable and large data sizes
Real-world data sizes are often unpredictable, so dynamic structures provide the scalability and flexibility needed.
Q11. Static data structures can have their values changed, but their size is fixed.
BeginnerShow answer & explanation
True
Static refers to the fixed size, not immutability. You can modify elements in a static array.
Q12. Dynamic data structures always use non-contiguous memory.
BeginnerShow answer & explanation
False
While linked lists are non-contiguous, dynamic arrays can use contiguous memory while still being dynamic in size.
Q13. What is the fundamental difference between static and dynamic data structures?
BeginnerShow answer & explanation
—
This is the core distinction based on memory allocation time and size flexibility.
Q14. Name two examples of static data structures.
BeginnerShow answer & explanation
—
These have fixed sizes determined at compile time.
Q15. Name two examples of dynamic data structures.
BeginnerShow answer & explanation
—
These can grow or shrink at runtime by allocating and deallocating nodes.
Q16. Why is access faster in static data structures?
BeginnerShow answer & explanation
—
Contiguous memory enables O(1) access to any element by index.
Q17. What is a major drawback of dynamic data structures?
BeginnerShow answer & explanation
—
Manual memory management adds complexity and risk.
Q18. In C, which functions are used to allocate dynamic memory?
BeginnerShow answer & explanation
—
These are standard library functions for runtime memory allocation.
Q19. What is the real-life analogy for a static data structure?
BeginnerShow answer & explanation
—
This illustrates the fixed-size nature and potential wastage.
Q20. How does a linked list store its elements?
BeginnerShow answer & explanation
—
Nodes are scattered in RAM and connected via pointers.
Q21. Which of the following is a data item?
BeginnerShow answer & explanation
42
A data item is a single unit of value, like 42. Student record, file, and group are collections.
Q22. What is a group item?
BeginnerShow answer & explanation
A collection of related data items
A group item is a collection of related data items, like a point (x, y). A file is a collection of records, not a group item.
Q23. In a student record, 'Name' is an example of a:
BeginnerShow answer & explanation
Field
A field is a single attribute of a record. 'Name' is one attribute, so it's a field.
Q24. Which of the following best describes a data structure?
BeginnerShow answer & explanation
A logical model for organizing data
A data structure is a logical model for organizing data for efficient access and modification. Storage structure is the physical layout.
Q25. What is the storage structure of an array?
BeginnerShow answer & explanation
Contiguous memory
An array uses contiguous memory locations, while a linked list uses scattered memory with pointers.
Q26. Which operation is NOT typically performed on a data structure?
BeginnerShow answer & explanation
Compile
Compile is a programming language operation, not a data structure operation. Insert, delete, search, and traverse are common operations.
Q27. In the C code `struct Student { int roll; char name[20]; float marks; };`, what does `struct Student` represent?
BeginnerShow answer & explanation
A record
The struct defines a record with fields. An array of such structs would be a file.
Q28. Which of the following is an example of a file?
BeginnerShow answer & explanation
An array of student records
A file is a collection of related records. An array of student records is a file.
Q29. Why might a linked list be preferred over an array for frequent insertions?
BeginnerShow answer & explanation
Insertion only requires updating pointers
In a linked list, insertion only requires updating pointers, avoiding the need to shift elements as in an array. This makes insertion faster.
Q30. What is the relationship between a data structure and a storage structure?
BeginnerShow answer & explanation
The data structure is abstract; the storage structure is concrete
A data structure is a logical model (abstract), while a storage structure is the physical implementation (concrete).
Q31. Which of the following correctly orders the hierarchy from smallest to largest?
BeginnerShow answer & explanation
Data item -> Group item -> Record -> File
The hierarchy is data item (smallest) -> group item -> record -> file (largest).
Q32. A data structure and its storage structure are always identical.
BeginnerShow answer & explanation
False
A data structure is a logical model, while a storage structure is the physical implementation. They are distinct concepts.
Q33. A record is a collection of fields, and a file is a collection of records.
BeginnerShow answer & explanation
True
This is the standard definition: a record is a complete collection of fields, and a file is a collection of related records.
Q34. Define 'data' and give an example.
BeginnerShow answer & explanation
—
Data is the general term for raw values.
Q35. What is a data item? Provide an example.
BeginnerShow answer & explanation
—
A data item is a specific single value.
Q36. Explain the concept of a group item with an example.
BeginnerShow answer & explanation
—
Group items combine related data items.
Q37. What is a field? Give an example.
BeginnerShow answer & explanation
—
Fields are the individual attributes.
Q38. What is a record? Provide an example.
BeginnerShow answer & explanation
—
A record represents one entity with all its attributes.
Q39. What is a file in the context of data structures?
BeginnerShow answer & explanation
—
Files group related records together.
Q40. Define data structure.
BeginnerShow answer & explanation
—
It's the abstract organization of data.
Q41. Define storage structure.
BeginnerShow answer & explanation
—
It's the physical implementation in memory.
Q42. List four common operations on data structures.
BeginnerShow answer & explanation
—
These are the basic operations.
Q43. Which of the following best defines a data structure?
BeginnerShow answer & explanation
A specialized format for organizing, storing, and managing data
A data structure is a specialized format for organizing, storing, and managing data efficiently. It is not a language feature, algorithm, or hardware.
Q44. Which of the following is NOT a common operation on a data structure?
BeginnerShow answer & explanation
Compile
Common operations include insert, delete, search, traverse, and sort. Compiling is a process of translating code, not an operation on data structures.
Q45. How does an array store its elements in memory?
BeginnerShow answer & explanation
In contiguous memory locations
Arrays store elements in contiguous memory locations, meaning elements are placed next to each other. This allows direct access by index.
Q46. Which data structure uses pointers to link elements stored in scattered memory?
BeginnerShow answer & explanation
Linked list
A linked list stores elements in scattered memory locations, with each element containing a pointer to the next element. Arrays use contiguous memory.
Q47. What is the main advantage of using a data structure over separate variables?
BeginnerShow answer & explanation
It brings efficiency, clarity, and organization to programs
Data structures organize data so that operations become efficient and code becomes clear and maintainable. They do not always use less memory and still require algorithms.
Q48. Which access method allows direct retrieval of an element using its index?
BeginnerShow answer & explanation
Direct
Direct access uses an index to retrieve an element immediately, as in arrays. Sequential access requires traversing elements one by one.
Q49. What is the relationship between data structures and algorithms?
BeginnerShow answer & explanation
Data structures organize data; algorithms manipulate data
Data structures define how data is organized, while algorithms are step-by-step procedures to process that data. They work together.
Q50. Which of the following is a real-world application of a stack?
BeginnerShow answer & explanation
Undo feature in a text editor
Stacks are used for undo features because they follow Last-In-First-Out (LIFO) order, allowing the most recent action to be undone first.
Q51. What is the main trade-off between arrays and linked lists?
BeginnerShow answer & explanation
Arrays allow fast access by index but slow insert/delete; linked lists allow fast insert/delete but slow access by index
Arrays provide O(1) access by index but require shifting elements for insert/delete. Linked lists allow O(1) insert/delete at known positions but require traversal for access.
Q52. Why is choosing the right data structure important?
BeginnerShow answer & explanation
It balances memory usage and performance for the required operations
Different data structures have different strengths and weaknesses. Choosing the right one optimizes memory usage and performance for the specific operations needed.
Q53. Which data structure is best suited for implementing a navigation app that finds the shortest route?
BeginnerShow answer & explanation
Graph
Graphs represent roads and intersections, and algorithms like Dijkstra's work on graphs to find the shortest path.
Q54. What is a key characteristic of a data structure's formal definition?
BeginnerShow answer & explanation
It defines how data is grouped, what operations are allowed, and how data is stored in memory
A data structure is a logical and mathematical model that defines grouping, operations, storage representation, and efficiency impact.
Q55. A linked list stores elements in contiguous memory locations.
BeginnerShow answer & explanation
False
A linked list stores elements in scattered memory locations with pointers linking them, not contiguous memory.
Q56. Data structures and algorithms are the same concept.
BeginnerShow answer & explanation
False
Data structures organize data, while algorithms are step-by-step procedures to manipulate data. They are distinct but complementary.
Q57. Define a data structure in your own words.
BeginnerShow answer & explanation
—
Full marks require mentioning organization, storage, and efficiency.
Q58. List the four key components of a data structure.
BeginnerShow answer & explanation
—
Each component is essential: data values, how they are stored, what operations can be performed, and how they are accessed.
Q59. Name five common operations that can be performed on a data structure.
BeginnerShow answer & explanation
—
These are the fundamental operations that data structures support.
Q60. What are the three access methods for data structures?
BeginnerShow answer & explanation
—
Direct access uses an index, sequential access uses a loop, and recursive access uses recursion.
Q61. What are the benefits of using data structures in programming?
BeginnerShow answer & explanation
—
List the three main benefits.
Q62. How does an array allow direct access to its elements?
BeginnerShow answer & explanation
—
Mention contiguous memory and index calculation.
Q63. Which of the following is a linear data structure?
BeginnerShow answer & explanation
Queue
Queue is a linear structure that follows FIFO. Trees, graphs, and tries are non-linear.
Q64. Which data structure uses LIFO (Last In, First Out) principle?
BeginnerShow answer & explanation
Stack
Stack follows LIFO; the last element added is the first to be removed.
Q65. In a linear data structure, each element (except the first and last) has:
BeginnerShow answer & explanation
Exactly one predecessor and one successor
Linear structures have a one-to-one relationship: each element has exactly one predecessor and one successor, except the ends.
Q66. Which of the following is a non-linear data structure?
BeginnerShow answer & explanation
Graph
Graph is non-linear because elements can have many-to-many relationships.
Q67. What is the primary characteristic of a non-linear data structure?
BeginnerShow answer & explanation
Elements are arranged hierarchically or as a graph
Non-linear structures arrange elements hierarchically (tree) or as a network (graph), allowing multiple connections.
Q68. Which real-world scenario best represents a queue data structure?
BeginnerShow answer & explanation
People waiting in a line
A queue follows FIFO, like people waiting in a line: the first person in line is served first.
Q69. Which data structure is best suited for modeling a social media network?
BeginnerShow answer & explanation
Graph
A social network has many-to-many relationships, which a graph represents naturally with nodes and edges.
Q70. What is a key difference in memory allocation between linear and non-linear structures?
BeginnerShow answer & explanation
Linear structures can use contiguous or linked memory; non-linear mostly use dynamic pointer-based memory
Linear structures like arrays use contiguous memory, while linked lists use pointers. Non-linear structures like trees and graphs typically use dynamic memory.
Q71. Which of the following statements about trees and graphs is TRUE?
BeginnerShow answer & explanation
A tree is a special type of graph with no cycles and a single root
A tree is a connected acyclic graph with a root and exactly one path between any two nodes. Graphs can have cycles and multiple paths.
Q72. Which structure is most efficient for autocomplete features?
BeginnerShow answer & explanation
Trie
Tries are designed for efficient word searching and prefix matching, making them ideal for autocomplete.
Q73. What is the time complexity for searching in a balanced binary search tree?
BeginnerShow answer & explanation
$O(\log n)$
A balanced BST keeps height logarithmic, so search is $O(\log n)$.
Q74. A linked list is a non-linear data structure.
BeginnerShow answer & explanation
False
A linked list is linear because each node points to exactly one next node, forming a sequence.
Q75. Non-linear data structures are always slower than linear ones.
BeginnerShow answer & explanation
False
Non-linear structures like balanced trees can be faster for search operations than linear structures like linked lists.
Q76. Define linear data structure and give two examples.
BeginnerShow answer & explanation
—
This definition captures the sequential layout and one-to-one relationship.
Q77. Define non-linear data structure and give two examples.
BeginnerShow answer & explanation
—
Non-linear structures have one-to-many or many-to-many relationships.
Q78. What is the difference between LIFO and FIFO?
BeginnerShow answer & explanation
—
These principles define the order of operations in stacks and queues.
Q79. Give a real-world analogy for a stack and explain why it fits.
BeginnerShow answer & explanation
—
Stack's LIFO behavior matches the undo feature.
Q80. Give a real-world analogy for a queue and explain why it fits.
BeginnerShow answer & explanation
—
Queue's FIFO behavior matches a physical queue.
Q81. What is the relationship between elements in a linear data structure?
BeginnerShow answer & explanation
—
This is a defining characteristic of linear structures.
Q82. What type of memory allocation is typical for non-linear data structures?
BeginnerShow answer & explanation
—
Because the structure grows and shrinks as connections change.
Q83. Name three real-world analogies for non-linear data structures.
BeginnerShow answer & explanation
—
These analogies illustrate hierarchical or network-like relationships.
Q84. Which of the following is a primitive data type?
BeginnerShow answer & explanation
`int`
Primitive data types are basic types provided by the language, such as `int`, `char`, `float`, `bool`, and `pointer`. Arrays, stacks, and trees are non-primitive because they organize multiple values.
Q85. Which of the following is a non-primitive data structure?
BeginnerShow answer & explanation
Queue
A queue is a linear non-primitive data structure. `float`, `bool`, and `char` are primitive types that store single atomic values.
Q86. What does a primitive data structure store?
BeginnerShow answer & explanation
A single atomic value
Primitive data structures represent atomic, single-value data types. They cannot hold multiple values; that is the role of non-primitive structures.
Q87. Which of the following is an example of a non-linear non-primitive data structure?
BeginnerShow answer & explanation
Tree
Non-linear structures arrange elements hierarchically or interconnectedly. Trees, graphs, heaps, and tries are non-linear. Arrays, stacks, and queues are linear.
Q88. Which of the following is a linear non-primitive data structure?
BeginnerShow answer & explanation
Linked List
Linear data structures arrange elements in a sequence. Linked lists, arrays, stacks, and queues are linear. Graphs, heaps, and tries are non-linear.
Q89. Which of the following statements about arrays is true?
BeginnerShow answer & explanation
Arrays are non-primitive because they organize multiple elements.
Arrays are non-primitive because they organize multiple elements into a single structure, regardless of the type of elements they store. The fact that they store primitive values does not make them primitive.
Q90. What is the main advantage of non-primitive data structures over primitive ones?
BeginnerShow answer & explanation
They can organize and manipulate collections of data.
Non-primitive structures are designed for manipulation, searching, sorting, and organizing collections of data. Primitives are simple and fast for single values but cannot handle collections.
Q91. Which of the following is a real-life analogy for non-primitive data structures?
BeginnerShow answer & explanation
A library
A library is an organized collection of books, similar to how non-primitive structures organize multiple values. A single brick, book, or chair represents a primitive unit.
Q92. How does memory usage typically differ between primitive and non-primitive data structures?
BeginnerShow answer & explanation
Primitives use fixed memory; non-primitives can use dynamic memory.
Primitive data types have a fixed memory footprint (e.g., an `int` always uses a fixed number of bytes). Non-primitive structures can use dynamic memory, allowing them to grow or shrink as needed.
Q93. Which of the following is an example of a primitive data type used to store a memory address?
BeginnerShow answer & explanation
`pointer`
A pointer is a primitive data type that stores memory addresses. It is often used to build dynamic data structures like linked lists.
Q94. Why are non-primitive data structures considered more flexible than primitive ones?
BeginnerShow answer & explanation
They can grow, shrink, and reorganize.
Non-primitive structures like linked lists can dynamically adjust their size, whereas primitives have a fixed size. This flexibility allows them to handle varying amounts of data efficiently.
Q95. Which of the following best describes the relationship between primitive and non-primitive data structures?
BeginnerShow answer & explanation
Non-primitive structures are built using primitive types as building blocks.
Non-primitive data structures are composed of primitive types. For example, an array of integers uses `int` as its base type. Primitives are the atoms; non-primitives are the molecules.
Q96. Which of the following is NOT a characteristic of primitive data structures?
BeginnerShow answer & explanation
They can grow and shrink dynamically.
Primitive data structures have fixed memory and cannot grow or shrink. Dynamic behavior is a feature of many non-primitive structures, such as linked lists.
Q97. Which of the following is an example of a non-primitive data structure that can hold a collection of student records?
BeginnerShow answer & explanation
Linked list
A linked list is a non-primitive structure that can hold multiple records, each composed of primitives like name, age, and grade. Primitives like `int` or `char` can only hold a single value.
Q98. What is the primary purpose of non-primitive data structures?
BeginnerShow answer & explanation
To organize and manipulate collections of data
Non-primitive data structures are designed for organizing, storing, and manipulating collections of data. They support operations like searching, sorting, and dynamic resizing, which are essential for complex applications.
Q99. Arrays are considered primitive data structures because they store primitive values.
BeginnerShow answer & explanation
False
Arrays are non-primitive because they organize multiple elements into a single structure. Storing primitive values does not make them primitive.
Q100. Non-primitive data structures can be either linear or non-linear.
BeginnerShow answer & explanation
True
Non-primitive structures are classified into linear (e.g., arrays, stacks, queues, linked lists) and non-linear (e.g., trees, graphs, heaps, tries).
Q101. Define primitive data structure and give two examples.
BeginnerShow answer & explanation
—
This tests recall of the definition and examples.
Q102. Define non-primitive data structure and give two examples.
BeginnerShow answer & explanation
—
This tests recall of the definition and examples.
Q103. What are the two categories of non-primitive data structures?
BeginnerShow answer & explanation
—
This tests understanding of the classification.
Q104. What is the real-life analogy of primitive vs non-primitive data structures?
BeginnerShow answer & explanation
—
This tests the ability to relate abstract concepts to real-world examples.
Q105. What is the primary use case for primitive data structures?
BeginnerShow answer & explanation
—
This tests understanding of when to use primitives.
Q106. List three primitive data types and their typical use.
BeginnerShow answer & explanation
—
This tests recall of primitive types.
Q107. List three non-primitive data structures and their typical use.
BeginnerShow answer & explanation
—
This tests recall of non-primitive structures.
Q108. Give an example of a linear non-primitive data structure and a non-linear one.
BeginnerShow answer & explanation
—
This tests the ability to classify non-primitive structures.
Q109. Which data structure uses pointers to link elements stored in scattered memory?
BeginnerShow answer & explanation
Linked list
A linked list stores elements in scattered memory locations, with each element containing a pointer to the next element. Arrays use contiguous memory.
Q110. What is the main advantage of using a data structure over separate variables?
BeginnerShow answer & explanation
It brings efficiency, clarity, and organization to programs
Data structures organize data so that operations become efficient and code becomes clear and maintainable. They do not always use less memory and still require algorithms.
Q111. Which of the following best defines a data structure?
BeginnerShow answer & explanation
A specialized format for organizing, storing, and managing data
A data structure is a specialized format for organizing, storing, and managing data efficiently. It is not a language feature, algorithm, or hardware.
Building up · 57 questions
Q112. What does 'static' refer to in a static data structure?
IntermediateShow answer & explanation
—
This clarifies a common misconception.
Q113. When should you prefer a static data structure?
IntermediateShow answer & explanation
—
Static structures are simpler and faster but inflexible.
Q114. What is a common risk when using dynamic structures?
IntermediateShow answer & explanation
—
Unreleased memory can exhaust system resources over time.
Q115. What is the trade-off between static and dynamic structures?
IntermediateShow answer & explanation
—
This is the key trade-off in choosing between them.
Q116. Why do most real-world applications prefer dynamic structures?
IntermediateShow answer & explanation
—
Real-world data sizes are often unpredictable.
Q117. A developer needs to store a fixed set of sensor readings collected every second for exactly 60 seconds. Which data structure would be most appropriate and why?
IntermediateShow answer & explanation
A static array, because the size is known in advance and access is faster.
Since the number of readings is fixed at 60, a static array is ideal: it provides fast O(1) access and avoids the overhead of dynamic memory management. A linked list or dynamic array would add unnecessary complexity and slower access without any benefit because the size is known and will not change.
Q118. Which of the following best explains why accessing an element in a static array is faster than in a linked list?
IntermediateShow answer & explanation
Static arrays store elements contiguously, enabling direct arithmetic to compute an element's address.
In a static array, elements are stored in contiguous memory locations. The address of any element can be computed directly using the base address and index (e.g., base + index * size), giving O(1) access. A linked list requires traversing pointers from the head to reach a specific node, which is O(n).
Q119. A programmer uses `malloc()` to create a linked list node but forgets to call `free()` after the node is no longer needed. What is the most likely consequence?
IntermediateShow answer & explanation
The memory remains allocated but is no longer accessible, leading to a memory leak.
When `malloc()` allocates memory, it must be explicitly released with `free()`. If not, the memory stays allocated but the pointer is lost, making it impossible to free later. This is a memory leak, which gradually consumes available memory and can eventually cause the program or system to run out of memory.
Q120. Which of the following scenarios would most likely require a dynamic data structure?
IntermediateShow answer & explanation
Storing user-generated posts on a social media platform where the number of posts changes constantly.
Social media posts are unpredictable in number and change frequently, so a dynamic structure like a linked list or dynamic array is needed to grow and shrink as posts are added or removed. The other options have known, fixed sizes, making static structures more appropriate.
Q121. A dynamic array (e.g., using `realloc` in C) can be resized at runtime. Which of the following statements correctly describes its memory layout?
IntermediateShow answer & explanation
It uses contiguous memory, but resizing may require copying all elements to a new memory block.
A dynamic array maintains a contiguous block of memory. When resizing is needed, `realloc` attempts to extend the existing block; if that's not possible, it allocates a new contiguous block and copies the elements over. Thus, the memory remains contiguous, but resizing can be costly due to the copy operation.
Q122. In the C struct `struct Student { int roll; char name[20]; float marks; };`, identify the fields.
IntermediateShow answer & explanation
—
Fields are the individual attributes of the record.
Q123. In the C struct `struct Student { int roll; char name[20]; float marks; };`, what does the struct represent?
IntermediateShow answer & explanation
—
The struct defines a record with fields.
Q124. How would you represent a file of student records in C?
IntermediateShow answer & explanation
—
An array of structs is a collection of related records, i.e., a file.
Q125. Explain the difference between a data structure and a storage structure.
IntermediateShow answer & explanation
—
The same logical structure can have different physical implementations.
Q126. Give an example of a logical data structure that can have multiple storage structures.
IntermediateShow answer & explanation
—
The logical behavior (LIFO) remains the same, but the storage differs.
Q127. In the C struct `struct Student { int roll; char name[20]; float marks; };`, which of the following correctly identifies the roles of `roll`, `name`, and `marks`?
IntermediateShow answer & explanation
They are fields because each is a single attribute of a record.
In the context of a record, `roll`, `name`, and `marks` are individual attributes, which are called fields. A record is the entire struct (one student), and a group item is a collection of related data items, but here they are specifically fields of a record.
Q128. Which of the following best explains why a linked list is often preferred over an array for frequent insertions and deletions?
IntermediateShow answer & explanation
A linked list requires only updating pointers for insertion/deletion, not shifting elements.
In a linked list, each node stores data and a pointer to the next node. Inserting or deleting a node involves adjusting a few pointers, whereas an array requires shifting elements to maintain contiguous storage. This makes linked lists more efficient for frequent insertions/deletions.
Q129. Given the logical data structure 'stack', which of the following statements about its storage structure is correct?
IntermediateShow answer & explanation
A stack can be implemented using either an array or a linked list, with different trade-offs.
A stack is a logical data structure with LIFO behavior. It can be implemented using an array (contiguous memory, fixed size) or a linked list (scattered nodes, dynamic size). The logical model is separate from the physical storage, so multiple storage structures are possible.
Q130. In a library catalog, each book is represented with fields like title, author, and ISBN. The entire catalog is a collection of all such book entries. Which term best describes the entire catalog?
IntermediateShow answer & explanation
A file
A file is defined as a collection of related records. In this example, each book entry is a record, and the entire catalog (all book entries) is a file. A group item is a collection of related data items, not necessarily records, and a data item is a single value.
Q131. Why is the choice of storage structure important for the performance of operations on a data structure?
IntermediateShow answer & explanation
Because different storage structures have different time and space trade-offs for operations like access and insertion.
The storage structure (e.g., array vs. linked list) affects how operations are implemented. Arrays provide O(1) random access but O(n) insertion/deletion, while linked lists provide O(1) insertion/deletion (given a pointer) but O(n) access. These trade-offs directly impact performance, so the choice matters.
Q132. Explain the difference between contiguous and scattered memory storage.
IntermediateShow answer & explanation
—
Mention the trade-off between access speed and modification flexibility.
Q133. Why are data structures important for program efficiency?
IntermediateShow answer & explanation
—
Highlight the contrast between organized and unorganized data.
Q134. What is the difference between a data structure and an algorithm?
IntermediateShow answer & explanation
—
Clarify that they are distinct but complementary.
Q135. Describe the trade-off between arrays and linked lists.
IntermediateShow answer & explanation
—
Mention both access speed and modification flexibility.
Q136. Give two real-world applications of data structures and identify the data structure used.
IntermediateShow answer & explanation
—
Any valid example from the source material is acceptable.
Q137. What are the formal characteristics of a data structure?
IntermediateShow answer & explanation
—
Include grouping, operations, storage, and efficiency.
Q138. Explain how a stack is used in an undo feature.
IntermediateShow answer & explanation
—
Mention LIFO and push/pop operations.
Q139. What is the role of storage representation in a data structure?
IntermediateShow answer & explanation
—
Mention contiguous vs. scattered and performance impact.
Q140. A programmer needs to store a collection of student records and frequently insert new records at the beginning of the collection. Which data structure would be most efficient for this operation, and why?
IntermediateShow answer & explanation
Linked list, because insertion at the head only requires updating a few pointers.
Inserting at the beginning of an array requires shifting all existing elements, which is O(n). A linked list can insert at the head in O(1) by updating the head pointer and the new node's next pointer. Stacks and hash tables are not designed for arbitrary insertion at the beginning.
Q141. Consider the following scenario: You need to implement a feature that finds the shortest path between two locations in a city. Which data structure is most appropriate to model the city's road network?
IntermediateShow answer & explanation
Graph
A graph is the natural data structure to represent a road network, with intersections as vertices and roads as edges. Algorithms like Dijkstra's operate on graphs to find shortest paths. Arrays, linked lists, and stacks do not capture the complex relationships between locations.
Q142. Which of the following best explains the relationship between a data structure and an algorithm?
IntermediateShow answer & explanation
A data structure organizes data, and an algorithm is a procedure that operates on that data.
A data structure is the logical organization of data (e.g., array, tree), while an algorithm is a step-by-step procedure to perform a task (e.g., binary search). They work together: algorithms often require specific data structures to be efficient.
Q143. A developer is choosing between an array and a linked list to store a list of tasks. The tasks are frequently searched by their position (e.g., the 5th task), but rarely inserted or deleted. Which data structure is more suitable, and why?
IntermediateShow answer & explanation
Array, because it allows direct access by index in O(1) time.
Arrays provide O(1) direct access by index, which is ideal for frequent positional access. Linked lists require O(n) traversal to reach a specific position. While arrays may have overhead for insertions/deletions, the scenario emphasizes searching by position, making arrays the better choice.
Q144. In the context of data structures, what does 'storage representation' refer to?
IntermediateShow answer & explanation
How data is physically laid out in memory, such as contiguous or scattered.
Storage representation describes how data is placed in memory—contiguously (like arrays) or scattered with pointers (like linked lists). This affects access speed and flexibility. The logical relationship and operations are separate aspects of a data structure.
Q145. What is a trie and where is it commonly used?
IntermediateShow answer & explanation
—
Tries allow fast prefix-based searches.
Q146. How does traversal differ between linear and non-linear structures?
IntermediateShow answer & explanation
—
This affects how you implement algorithms to visit all elements.
Q147. Why might a balanced binary search tree be faster than a linked list for searching?
IntermediateShow answer & explanation
—
The tree's structure allows halving the search space each step.
Q148. What is the key difference between a tree and a graph?
IntermediateShow answer & explanation
—
All trees are graphs, but not all graphs are trees.
Q149. When would you choose a linear structure over a non-linear one?
IntermediateShow answer & explanation
—
Linear structures are easier to implement and traverse for sequential processing.
Q150. When would you choose a non-linear structure?
IntermediateShow answer & explanation
—
Non-linear structures model one-to-many or many-to-many relationships.
Q151. What is a common misconception about non-linear data structures?
IntermediateShow answer & explanation
—
Efficiency depends on the operation and the specific structure.
Q152. A developer needs to store a collection of words for an autocomplete feature. The words share common prefixes, and the developer wants to minimize memory usage by sharing those prefixes. Which data structure is the most appropriate choice, and why?
IntermediateShow answer & explanation
A trie, because it is a non-linear structure that stores characters in a tree-like layout, sharing common prefixes.
A trie is a non-linear, tree-like data structure specifically designed for storing strings with common prefixes. It shares those prefixes, reducing memory usage and enabling fast prefix-based searches, which is ideal for autocomplete. The other options are linear structures that do not share prefixes and would store ea…
Q153. Consider a binary search tree (BST) and a sorted array. Which statement correctly compares their performance for search and insertion operations?
IntermediateShow answer & explanation
A sorted array has $O(\log n)$ search time but $O(n)$ insertion time, while a balanced BST has $O(\log n)$ for both search and insertion.
A sorted array supports binary search in $O(\log n)$ time, but inserting an element requires shifting elements, which is $O(n)$. A balanced BST maintains $O(\log n)$ time for both search and insertion because it keeps the tree height logarithmic. This trade-off is a key difference between linear and non-linear structur…
Q154. A file system directory structure is best modeled as a tree, not a graph. Which property of a tree makes it more suitable than a general graph for this use case?
IntermediateShow answer & explanation
A tree has a single root and exactly one path between any two nodes, ensuring a clear hierarchy without cycles.
A tree is a non-linear, hierarchical structure with a single root and exactly one path between any two nodes. This ensures no cycles and a clear parent-child relationship, which matches how directories contain subdirectories. A general graph can have cycles and multiple paths, which would not represent a strict hierarc…
Q155. In a linked list, each node points to the next node. Why is a linked list still considered a linear data structure even if it uses pointers and dynamic memory allocation?
IntermediateShow answer & explanation
Because each element has exactly one predecessor and one successor, forming a single sequence.
The linearity of a data structure is defined by the logical arrangement of elements, not the physical memory layout. In a linked list, each node (except the first and last) has exactly one predecessor and one successor, so elements form a single sequence. This is true even though memory is dynamically allocated and nod…
Q156. A social network like Facebook uses a graph to model friendships. Which characteristic of a graph makes it more suitable than a tree for this scenario?
IntermediateShow answer & explanation
A graph allows many-to-many relationships and cycles, representing mutual friendships and multiple connections.
A graph is a non-linear structure that allows many-to-many relationships and cycles. In a social network, a person can have many friends, and friendships can form cycles (e.g., A is friends with B, B with C, C with A). A tree, by contrast, has a hierarchical structure with exactly one path between nodes and no cycles,…
Q157. Why is an array considered non-primitive even though it stores primitive values?
IntermediateShow answer & explanation
—
This targets the common misconception about arrays.
Q158. How does memory usage differ between primitive and non-primitive data structures?
IntermediateShow answer & explanation
—
This tests understanding of memory allocation.
Q159. Which type of data structure offers higher flexibility: primitive or non-primitive? Explain.
IntermediateShow answer & explanation
—
This tests the flexibility characteristic.
Q160. What is the primary use case for non-primitive data structures?
IntermediateShow answer & explanation
—
This tests understanding of when to use non-primitives.
Q161. What does 'atomic' mean in the context of primitive data types?
IntermediateShow answer & explanation
—
This tests understanding of the term 'atomic'.
Q162. Why are non-primitive data structures considered 'user-defined'?
IntermediateShow answer & explanation
—
This tests understanding of the origin of non-primitives.
Q163. What is the role of a pointer in data structures?
IntermediateShow answer & explanation
—
This tests understanding of pointers as primitives.
Q164. A programmer needs to store a collection of student records, each containing a name (string), an age (int), and a grade (char). Which approach best demonstrates the relationship between primitive and non-primitive data structures?
IntermediateShow answer & explanation
Use an array of structs, where each struct combines primitive types (string, int, char) into a single non-primitive unit.
Non-primitive data structures are built on top of primitives. An array of structs organizes multiple primitive values (name, age, grade) into a structured, indexable collection, demonstrating how non-primitives combine primitives. The other options either misuse primitives or incorrectly classify arrays as primitive.
Q165. Consider the following code snippet: ```c int arr[5] = {10, 20, 30, 40, 50}; ``` Why is `arr` considered a non-primitive data structure even though it stores primitive `int` values?
IntermediateShow answer & explanation
Because it holds multiple values and provides an organized, indexable collection.
The key distinction is that non-primitive structures organize multiple elements into a single unit. An array groups five `int` values into one indexable structure, making it non-primitive. It is not user-defined (arrays are built-in), and it has a fixed size, so the other options are incorrect.
Q166. In terms of memory usage, how do primitive and non-primitive data structures typically differ?
IntermediateShow answer & explanation
Primitive types use a fixed amount of memory, while non-primitive types can use dynamic memory and often require additional overhead for organization.
Primitive types have a fixed memory footprint (e.g., an `int` is always 4 bytes on many systems). Non-primitive structures like linked lists or arrays may use dynamic memory and often include extra overhead (e.g., pointers, metadata) to manage the collection. The other options misrepresent the typical memory behavior.
Q167. A developer needs to represent a hierarchical file system where each folder can contain subfolders and files. Which type of non-primitive data structure is most appropriate, and why?
IntermediateShow answer & explanation
A tree, because it arranges elements hierarchically with parent-child relationships.
A file system naturally forms a hierarchy—folders contain subfolders, which contain files. Trees are non-linear non-primitive structures designed for hierarchical relationships. Queues and stacks are linear structures for ordered processing, and arrays are linear collections, none of which capture the hierarchical natu…
Q168. Which of the following statements best explains why non-primitive data structures are considered 'user-defined'?
IntermediateShow answer & explanation
They are created by combining primitive types and other structures to suit specific needs.
Non-primitive structures are built by the programmer using primitives and other structures (e.g., a struct combining int and char). They are not predefined by the language; the user designs them to solve specific problems. The other options incorrectly describe their origin or flexibility.
Topper level · 32 questions
Q169. What is a memory leak?
AdvancedShow answer & explanation
—
It is a serious issue in long-running programs.
Q170. Can a dynamic structure use contiguous memory? Give an example.
AdvancedShow answer & explanation
—
This shows that dynamic refers to size flexibility, not necessarily memory layout.
Q171. A developer is designing a system to store sensor readings from a high-frequency data acquisition device. The device produces exactly 1,000 readings per second, and the system must process each batch of 1,000 readings within a strict 10-millisecond deadline. The readings are processed sequentially, and the size never changes. Which data structure should the developer choose, and why?
AdvancedShow answer & explanation
A static array because it provides O(1) access and avoids runtime allocation overhead, ensuring the strict deadline is met.
The data size is known and fixed (1,000 readings per batch), and the system has a strict performance deadline. A static array allocates memory at compile time, providing contiguous storage and O(1) access with no runtime allocation overhead. Dynamic structures like linked lists or `realloc` introduce runtime overhead a…
Q172. Consider the following C code snippet: ```c int *arr = (int*) malloc(10 * sizeof(int)); arr = (int*) realloc(arr, 20 * sizeof(int)); free(arr); ``` Which of the following statements correctly describes the memory behavior of this dynamic array?
AdvancedShow answer & explanation
The `realloc` function may extend the existing memory block in place or move it to a new location; in either case, the logical order of elements is preserved.
`realloc` attempts to resize the existing memory block. If there is enough contiguous space, it extends in place; otherwise, it allocates a new block, copies the old data, and frees the old block. In both cases, the resulting memory is contiguous and the logical order of elements is preserved. The `free` call after `re…
Q173. A programmer needs to implement a function that processes a list of integers. The list is built by reading integers from a file until the end of the file is reached. The number of integers is unknown in advance. The programmer decides to use a linked list. Which of the following is the most significant trade-off of this choice compared to using a dynamic array?
AdvancedShow answer & explanation
The linked list uses more memory per element due to the pointer field, and accessing the i-th element requires O(n) time.
A linked list allocates memory dynamically for each node, but each node also stores a pointer to the next node, increasing memory overhead. Accessing the i-th element requires traversing from the head, which is O(n). A dynamic array (using `realloc`) also resizes at runtime but provides O(1) access and uses less memory…
Q174. A developer is writing a program that needs to store a fixed set of configuration parameters (e.g., 10 integer values) that are read at startup and never change in size. The program is running on a memory-constrained embedded system. Which of the following statements best justifies using a static array over a dynamic structure?
AdvancedShow answer & explanation
A static array avoids the overhead of runtime memory allocation, reducing the risk of memory leaks and ensuring predictable memory usage.
Static arrays allocate memory at compile time, so there is no runtime allocation overhead, no risk of memory leaks from `malloc`/`free`, and memory usage is fixed and predictable. This is ideal for memory-constrained systems where the size is known. Dynamic structures would introduce overhead and potential fragmentatio…
Q175. A software engineer is designing a text editor that must support undo/redo operations. The number of undo steps is limited to 50, and each step stores a snapshot of the document. The engineer initially uses a linked list to store the snapshots but notices that accessing the most recent snapshot (the last element) is slow. Which of the following changes would best improve the performance of accessing the most recent snapshot while still respecting the fixed limit of 50 steps?
AdvancedShow answer & explanation
Replace the linked list with a static array of size 50, using a circular buffer to overwrite the oldest snapshot when full.
A static array of size 50 with a circular buffer provides O(1) access to any element, including the most recent, and respects the fixed limit of 50 steps. Adding a tail pointer to a linked list would give O(1) access to the last node, but the question asks for the best improvement; a circular buffer is simpler and avoi…
Q176. Why is the choice of storage structure important for performance?
AdvancedShow answer & explanation
—
Performance depends on the storage structure.
Q177. Consider the following C code snippet: ```c struct Point { int x; int y; }; struct Point p1 = {3, 4}; struct Point p2 = {5, 6}; struct Point points[2] = {p1, p2}; ``` In the context of data structure terminology, which of the following statements is **most accurate**?
AdvancedShow answer & explanation
`points` is a file, `p1` and `p2` are records, and `x` and `y` are fields.
In the given code, `struct Point` defines a record with fields `x` and `y`. `p1` and `p2` are instances of that record, so they are records. The array `points` is a collection of related records (two points), which fits the definition of a file. The other options misclassify the terms: a data structure is a broader log…
Q178. A developer is designing a system to store a collection of employee records. Each record has fields: `emp_id`, `name`, and `salary`. The system must support frequent insertions and deletions of records in the middle of the collection, and also occasional searches by `emp_id`. Which storage structure is **most appropriate** for this scenario, and why?
AdvancedShow answer & explanation
A linked list, because insertions and deletions in the middle are O(1) once the position is known, and searches can be done by traversing.
The requirement emphasizes frequent insertions and deletions in the middle of the collection. A linked list excels at this because, once the node is located, insertion and deletion only involve updating pointers, which is O(1). Searches by `emp_id` would require traversal, but that is acceptable given the emphasis on i…
Q179. A data structure is defined as a stack, and it is implemented using an array. Which of the following statements correctly describes the relationship between the data structure and the storage structure?
AdvancedShow answer & explanation
The data structure is the abstract logical model (stack) with LIFO behavior, and the storage structure is the array that physically stores the elements.
The data structure is the logical organization—here, a stack with LIFO semantics. The storage structure is the concrete implementation—here, an array that holds the elements in memory. They are distinct concepts: the same logical stack could be implemented with a linked list, so they are not identical. The other option…
Q180. In a database system, a table `Students` has columns `id`, `name`, and `gpa`. Each row represents a student. Which of the following statements is **correct** in terms of data structure terminology?
AdvancedShow answer & explanation
Each row is a record, each column is a field, and the entire table is a file.
In a relational database, a row is a record because it is a collection of related fields that describe one entity (a student). Columns are fields because they represent a single attribute. The entire table is a file because it is a collection of related records. The other options misapply the hierarchy: a row is not a…
Q181. A programmer needs to store a collection of 2D points. Each point has two coordinates, `x` and `y`. The programmer defines a structure `Point` and then creates an array of `Point` objects. Which of the following statements is **most accurate**?
AdvancedShow answer & explanation
The structure `Point` is a record, each instance is a record, and the array is a file.
The structure `Point` defines a record with fields `x` and `y`. Each instance of `Point` is a record because it holds values for those fields. The array of `Point` objects is a collection of related records, which is a file. The other options misclassify: `Point` is not a group item (it has named fields, making it a re…
Q182. Why is choosing the right data structure important?
AdvancedShow answer & explanation
—
Emphasize the trade-offs and context-dependence.
Q183. A software engineer is designing a system to store a dynamic collection of student records. The system must support frequent insertions at the beginning of the collection, but also needs to allow binary search on the records. Which approach best satisfies both requirements, and why?
AdvancedShow answer & explanation
Use a balanced binary search tree (e.g., AVL tree) to maintain sorted order with O(log n) insertions and O(log n) search.
A balanced BST (like AVL) supports O(log n) insertions (including at the 'beginning' in sorted order) and O(log n) search, which is efficient. A linked list gives O(1) insertions at the head but binary search on a linked list is O(n) because random access is not possible. An array allows binary search but shifting elem…
Q184. Consider a navigation system that models a city's road network. The system must find the shortest path between two locations, and also frequently update road weights (e.g., due to traffic). Which data structure and algorithm combination is most appropriate, and why?
AdvancedShow answer & explanation
Use an adjacency list and Dijkstra's algorithm with a binary heap priority queue.
An adjacency list is memory-efficient for sparse road networks and allows quick updates to edge weights. Dijkstra's algorithm with a binary heap achieves O((V+E) log V) time, which is efficient for shortest path. An adjacency matrix uses O(V^2) memory and is inefficient for sparse graphs. DFS does not guarantee shortes…
Q185. A developer needs to implement an undo feature in a text editor. The editor must support undoing the most recent action, but also allow the user to redo an action after undoing. Which data structure(s) would be most appropriate, and what is the trade-off?
AdvancedShow answer & explanation
Use two stacks: one for undo and one for redo. When an undo occurs, the action is pushed onto the redo stack; when a new action is performed, the redo stack is cleared.
Two stacks are the classic approach: the undo stack holds actions in LIFO order, and the redo stack stores undone actions. When a new action is performed, the redo stack is cleared to maintain consistency. A single stack cannot support redo. A queue would give FIFO order, which is incorrect for undo. An array with a po…
Q186. A programmer is comparing an array and a linked list for storing a list of tasks. The tasks are frequently accessed by their position (e.g., the 5th task), but insertions and deletions are rare. Which data structure is more suitable, and what is the time complexity for accessing the 5th task in each?
AdvancedShow answer & explanation
Array, because accessing the 5th task is O(1) via index, while linked list access is O(n) due to traversal.
Arrays provide O(1) random access by index because elements are stored in contiguous memory. Linked lists require traversal from the head to reach the 5th node, which is O(n) in the worst case. Maintaining a pointer to the 5th node is not practical for arbitrary positions. Binary search on an array is O(log n) but only…
Q187. In the formal definition of a data structure, which of the following correctly describes the relationship between storage representation and the operations allowed?
AdvancedShow answer & explanation
Storage representation determines the efficiency of operations; for example, contiguous storage enables O(1) index access, while scattered storage requires traversal for access.
The storage representation (contiguous vs. scattered) directly impacts the time complexity of operations. Arrays (contiguous) allow O(1) random access, while linked lists (scattered) require O(n) traversal for access. Operations are not independent of storage; the physical layout determines how efficiently operations c…
Q188. A developer is designing a system to manage a sequence of undo operations in a text editor. The system must support undoing the most recent action first, but also allow the user to 'redo' an undone action if no new action is performed in between. Which combination of data structures would best model this behavior, and why?
AdvancedShow answer & explanation
Two stacks: one for undo history and one for redo history. When an action is undone, it is popped from the undo stack and pushed onto the redo stack. A new action clears the redo stack.
Undo/redo requires LIFO behavior for both operations: the most recent action is undone first, and the most recently undone action is redone first. Two stacks perfectly capture this: the undo stack holds actions in reverse chronological order, and the redo stack holds undone actions in reverse order of undoing. A new ac…
Q189. Consider a file system directory structure. It is modeled as a tree, not a general graph. Which property of a tree makes it more suitable than a general graph for representing the hierarchical organization of folders and files?
AdvancedShow answer & explanation
A tree has exactly one path between any two nodes, which enforces a strict parent-child relationship and prevents cycles, ensuring that there is no ambiguity in the hierarchy.
The defining property of a tree is that there is exactly one unique path between any two nodes, and it has a single root. This enforces a strict hierarchy where each node (except the root) has exactly one parent, making the structure acyclic. This is ideal for a file system because folders and files are organized in a…
Q190. A developer needs to store a collection of words for an autocomplete feature. The words share common prefixes, and the developer wants to minimize memory usage by sharing those prefixes. Which data structure is the most appropriate choice, and why?
AdvancedShow answer & explanation
A trie (prefix tree), because it stores common prefixes only once, reducing memory usage when many words share prefixes, and it allows efficient prefix-based searches.
A trie is specifically designed to store strings by sharing common prefixes. Each node represents a character, and paths from the root to leaves represent words. This minimizes memory usage when words share prefixes (e.g., 'cat', 'car', 'can' share 'ca'). It also supports efficient prefix searches: to find all words wi…
Q191. Consider a binary search tree (BST) and a sorted array. Which statement correctly compares their performance for search and insertion operations?
AdvancedShow answer & explanation
A sorted array supports O(log n) search via binary search but O(n) insertion due to shifting elements, while a balanced BST supports O(log n) for both search and insertion.
In a sorted array, binary search can find an element in O(log n) time, but inserting an element requires shifting all subsequent elements to maintain sorted order, which is O(n) in the worst case. A balanced BST (e.g., AVL or Red-Black tree) maintains balance, ensuring that both search and insertion are O(log n) becaus…
Q192. A social network like Facebook uses a graph to model friendships. Which characteristic of a graph makes it more suitable than a tree for this scenario?
AdvancedShow answer & explanation
Graphs allow cycles and multiple paths, which can represent mutual friendships and the possibility of multiple connection paths between users, whereas trees enforce a strict hierarchy with no cycles.
A social network is inherently non-hierarchical: friendships are mutual (undirected edges) and can form cycles (e.g., A is friends with B, B with C, C with A). There can be multiple paths between two users. Graphs are designed to model such many-to-many relationships with cycles and multiple paths. Trees, by definition…
Q193. A programmer is designing a system to store and manipulate a collection of sensor readings. Each reading is a floating-point number, and the system must support frequent insertions and deletions at both ends of the collection. Which combination of primitive and non-primitive data structures best satisfies the requirement, and why?
AdvancedShow answer & explanation
Use a linked list of `float` values because linked lists allow efficient insertions and deletions at both ends, and `float` is a primitive type.
A linked list is a non-primitive linear data structure that allows O(1) insertions and deletions at both ends (head and tail) if pointers are maintained. An array has fixed size and shifting elements for insertions/deletions is O(n). A stack only allows operations at one end (LIFO), and a queue typically allows inserti…
Q194. Consider the following C code snippet: ```c struct Student { char name[50]; int age; char grade; }; struct Student classRoom[30]; ``` Which of the following statements correctly analyzes the primitive and non-primitive aspects of this code?
AdvancedShow answer & explanation
`classRoom` is a non-primitive array of structures, and each structure is non-primitive because it contains a `char` array, but `age` and `grade` are primitive.
`classRoom` is an array (non-primitive) of `struct Student` objects. Each `struct Student` is a user-defined non-primitive data type because it aggregates multiple fields. Within the struct, `age` (int) and `grade` (char) are primitive types, while `name` is a `char` array (non-primitive). Arrays are never primitive; t…
Q195. A developer needs to represent a family tree where each person has a name (string), age (int), and a list of children. Which combination of primitive and non-primitive data structures is most appropriate, and why?
AdvancedShow answer & explanation
Use a tree (non-primitive) where each node stores a `string` for name and an `int` for age; children are pointers to other nodes. This leverages primitives for atomic data and non-primitives for hierarchy.
A family tree is a hierarchical structure, best represented by a tree (non-primitive, non-linear). Each node contains primitive data (string for name, int for age) and references to child nodes (pointers, which are primitive). Arrays and linked lists are linear and cannot naturally represent parent-child relationships.…
Q196. In a memory-constrained embedded system, a developer must choose between using a fixed-size array of `int` and a dynamically allocated linked list of `int` to store a list of sensor readings. Which statement best evaluates the trade-offs in terms of primitive vs non-primitive data structures?
AdvancedShow answer & explanation
The array is non-primitive and uses contiguous memory, leading to better cache locality and lower overhead, but it cannot grow dynamically; the linked list is non-primitive and uses scattered memory with per-node overhead, but it can grow dynamically.
Both arrays and linked lists are non-primitive data structures. Arrays use contiguous memory, which improves cache locality and reduces overhead, but have fixed size. Linked lists use non-contiguous memory with extra pointer storage per node, allowing dynamic growth but with higher memory overhead and poorer cache perf…
Q197. A programmer is implementing a text editor's undo feature. The editor needs to store a sequence of operations, each operation being a tuple (type: `char`, position: `int`, text: `string`). The undo feature must allow undoing the most recent operation first, and the number of operations can grow indefinitely. Which data structure choice best utilizes primitive and non-primitive types, and why?
AdvancedShow answer & explanation
Use a stack (non-primitive) where each element is a structure (non-primitive) containing primitive fields (`char`, `int`) and a string (non-primitive). This supports LIFO undo and dynamic growth.
Undo functionality requires LIFO (Last-In-First-Out) order, which is exactly what a stack provides. A stack is a non-primitive linear data structure that can grow dynamically. Each operation is a composite data type (struct/class) that aggregates primitive types (`char`, `int`) and a string (which is non-primitive). An…
Q198. A software engineer is designing a system to store a dynamic collection of student records. The system must support frequent insertions at the beginning of the collection, but also needs to allow binary search on the records. Which approach best satisfies both requirements, and why?
AdvancedShow answer & explanation
Use a balanced binary search tree (e.g., AVL tree) to maintain sorted order with O(log n) insertions and O(log n) search.
A balanced BST (like AVL) supports O(log n) insertions (including at the 'beginning' in sorted order) and O(log n) search, which is efficient. A linked list gives O(1) insertions at the head but binary search on a linked list is O(n) because random access is not possible. An array allows binary search but shifting elem…
Q199. Consider a navigation system that models a city's road network. The system must find the shortest path between two locations, and also frequently update road weights (e.g., due to traffic). Which data structure and algorithm combination is most appropriate, and why?
AdvancedShow answer & explanation
Use an adjacency list and Dijkstra's algorithm with a binary heap priority queue.
An adjacency list is memory-efficient for sparse road networks and allows quick updates to edge weights. Dijkstra's algorithm with a binary heap achieves O((V+E) log V) time, which is efficient for shortest path. An adjacency matrix uses O(V^2) memory and is inefficient for sparse graphs. DFS does not guarantee shortes…
Q200. A programmer is comparing an array and a linked list for storing a list of tasks. The tasks are frequently accessed by their position (e.g., the 5th task), but insertions and deletions are rare. Which data structure is more suitable, and what is the time complexity for accessing the 5th task in each?
AdvancedShow answer & explanation
Array, because accessing the 5th task is O(1) via index, while linked list access is O(n) due to traversal.
Arrays provide O(1) random access by index because elements are stored in contiguous memory. Linked lists require traversal from the head to reach the 5th node, which is O(n) in the worst case. Maintaining a pointer to the 5th node is not practical for arbitrary positions. Binary search on an array is O(log n) but only…
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages