C Programming Data Structures
- 38Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which characteristic is a primary limitation of arrays as a data structure?
Which characteristic is a primary limitation of arrays as a data structure?
38 questions
- 1
Which characteristic is a primary limitation of arrays as a data structure?
- A
They only allow storage of a single data type at one time.
- B
Arrays are not able to be used with other complex data structures.
- C
Their size must be determined and fixed before program execution.
- D
Accessing elements at random positions takes a significant amount of time.
Correct answer
CTheir size must be determined and fixed before program execution.
Explanation
Arrays, while simple and contiguous, require their size to be decided in advance, limiting their flexibility if more or less data is needed later. This is stated when discussing the catch with arrays, 'you have to decide in advance how big the array is going to be'.
- A
- 2
What is the main distinction between concrete data structures and abstract data types?
- A
Concrete data structures depend on specific implementation details.
- B
Concrete data structures are more broadly descriptive of functions.
- C
Abstract data types are always implemented using arrays in memory.
- D
Abstract data types focus on low-level memory allocation methods.
Correct answer
AConcrete data structures depend on specific implementation details.
Explanation
Concrete data structures are defined by their underlying implementation details (like variables and pointers), whereas abstract data types (ADTs) describe the logical model and operations without specifying how they are implemented. The content states: 'while data structures are something very concrete, involving variables and pointers and functions and the like, abstract data types are a little more broadly descriptive'.
- A
- 3
What real-world scenario effectively illustrates the concept of a queue?
- A
Documents waiting to be processed by a shared printer.
- B
Sweaters piled in a closet, taking the top one first.
- C
A stack of clean dishes in a cafeteria line.
- D
A collection of emails in an inbox, newest on top.
Correct answer
ADocuments waiting to be processed by a shared printer.
Explanation
A queue operates on a First-In, First-Out (FIFO) principle. Documents in a printer queue are processed in the order they were sent, mirroring the FIFO characteristic. The content explicitly mentions: 'if you're printing a bunch of things to a printer, those documents are generally stored in a queue so that they all print out in that precise order'.
- A
- 4
Which property correctly defines the operational behavior of a queue?
- A
Last In, Last Out (LILO) ordering for data elements.
- B
Last In, First Out (LIFO) ordering for data elements.
- C
First In, First Out (FIFO) ordering for data elements.
- D
Random access for removing elements from any position.
Correct answer
CFirst In, First Out (FIFO) ordering for data elements.
Explanation
Queues adhere to the First In, First Out (FIFO) principle, meaning the first element added to the queue is the first one to be removed. The content states: 'a queue or a line in the real world as we know it, is actually an example of a FIFO data type—First In, First Out'.
- A
- 5
What are the two fundamental operations typically associated with a queue in programming?
- A
Push to add and Pop to remove elements.
- B
Insert at middle and Delete from middle functions.
- C
Add to front and Remove from end operations.
- D
Enqueue to add and Dequeue to remove elements.
Correct answer
DEnqueue to add and Dequeue to remove elements.
Explanation
In the context of queues, elements are added using the 'enqueue' operation and removed using the 'dequeue' operation, maintaining the FIFO order. The content clarifies this: 'you would generally have two functions or two fundamental operations—enqueue, which means to put someone into the queue, and dequeue, which means the opposite to take someone out'.
- A
- 6
In a C struct for a queue, what is the purpose of storing both 'capacity' and 'size'?
- A
Capacity refers to current occupancy, size is the maximum possible.
- B
Capacity helps with memory leaks, size optimizes search operations.
- C
Capacity defines the fixed upper limit, size tracks current valid elements.
- D
Capacity indicates the number of active elements, size is for total memory.
Correct answer
CCapacity defines the fixed upper limit, size tracks current valid elements.
Explanation
Capacity represents the maximum number of items the queue can hold (often a constant), while size tracks the current number of valid items actively present in the queue. The content explains: 'Capacity is and always will be 50 because it's a const. But size is different, how, just intuitively? ... So size is going to be the current number of people or things in the queue'.
- A
- 7
What real-world example best illustrates the Last In, First Out (LIFO) principle?
- A
People forming a line to enter a movie theater.
- B
Printing documents to a shared office printer system.
- C
Items placed in a shopping cart for checkout order.
- D
A stack of trays in a dining hall, taking from the top.
Correct answer
DA stack of trays in a dining hall, taking from the top.
Explanation
A stack of trays in a dining hall demonstrates LIFO, as the last tray placed on top is typically the first one taken by a person. The content explicitly states: 'In the dining hall, in Annenberg Hall, there's a whole bunch of trays...people are constantly taking the ones higher up'.
- A
- 8
Which property correctly defines the operational behavior of a stack?
- A
First In, Last Out (FILO) for elements at all times.
- B
Random access to elements for quick retrieval and insertion.
- C
First In, First Out (FIFO) for all data interactions.
- D
Last In, First Out (LIFO) for data insertion and removal.
Correct answer
DLast In, First Out (LIFO) for data insertion and removal.
Explanation
Stacks operate on a Last In, First Out (LIFO) principle, meaning the last element added to the stack is the first one to be removed. The content states: 'stacks, therefore, suffice it to say, are not FIFO, First In, First Out. They're the world called LIFO—Last In, First Out'.
- A
- 9
What are the two primary operations for manipulating a stack?
- A
Insert to put and Extract to take out an item.
- B
Enqueue to add and Dequeue to remove elements.
- C
Append to end and Delete from front functions.
- D
Push to add to top and Pop to remove from top.
Correct answer
DPush to add to top and Pop to remove from top.
Explanation
Elements are added to a stack using the 'push' operation (placing them on top) and removed using the 'pop' operation (taking them from the top). The content says: 'instead of enqueue and dequeue in the world of stacks, we actually generally say push, which means to push something on top of the stack, and pop, which means to remove something from the top of the stack'.
- A
- 10
What is a significant drawback when an array needs to grow to accommodate more data?
- A
It always results in the deletion of existing data elements.
- B
The array can only store data of a single, fixed data type.
- C
The initial memory allocation for arrays is often insufficient.
- D
Copying elements to a larger array takes linear time complexity.
Correct answer
DCopying elements to a larger array takes linear time complexity.
Explanation
Growing an array typically involves allocating a new, larger array and copying all existing elements from the old array to the new one, which is a time-consuming operation (linear time). The content asks: 'what's a downside of this solution to growing an array to store more data after you first create it?' and later answers: 'it takes more time because now just to insert one value like the number 4, instead of just jumping to a free location and putting it there in constant time, I kind of have to incur linear time to copy the original array into a slightly bigger array and then add that new fourth value'.
- A
- 11
What is the primary trade-off when over-allocating memory for an array to avoid resizing?
- A
It makes the array's elements non-contiguous in memory.
- B
It leads to a substantial waste of available system memory.
- C
It significantly increases the complexity of the code.
- D
It slows down the initial creation time of the array.
Correct answer
BIt leads to a substantial waste of available system memory.
Explanation
Over-allocating memory ensures there's always space, but it can lead to inefficient use of memory if much of the allocated space remains unused. The content questions: 'what would be the downside of that approach sort of over allocating memory for an array? ... Waste of space.'
- A
- 12
What function in C simplifies the process of resizing an already allocated block of memory?
- A
The
callocfunction for zero-initialized memory. - B
The
freefunction for deallocating memory. - C
The
mallocfunction for new memory allocation. - D
The
reallocfunction for changing block size.
Correct answer
DThe
reallocfunction for changing block size.Explanation
reallocis a C function designed to resize a previously allocated block of memory. It attempts to expand or shrink the existing block or move it to a new location if necessary. The content explicitly states: 'we can use a function that we've not seen before called realloc and just ask really C to do the reallocation for me'. - A
- 13
What is the core concept that allows linked lists to overcome the contiguous memory requirement of arrays?
- A
Pre-allocating a maximum possible size for the entire list.
- B
Employing pointers to link non-contiguous memory locations.
- C
Storing all data in a single, large memory block.
- D
Using fixed-size memory segments for all elements.
Correct answer
BEmploying pointers to link non-contiguous memory locations.
Explanation
Linked lists use pointers to connect individual data elements (nodes) that can be located anywhere in memory, eliminating the need for contiguous storage. The content explains: 'I can just sort of follow arrows from one piece of data to another. And all we need for this is some mostly familiar syntax.'
- A
- 14
What is a primary disadvantage of using a linked list compared to an array?
- A
Linked lists require all elements to be of the same data type.
- B
Linked lists consume additional memory for storing pointers.
- C
Linked lists offer slower insertion and deletion operations.
- D
Linked lists can only be traversed in a single direction.
Correct answer
BLinked lists consume additional memory for storing pointers.
Explanation
While linked lists offer flexibility, they require extra memory to store the pointers that link nodes together. The content asks: 'What is the downside of linking them together here? ...It looks like it's twice as much space'.
- A
- 15
How is the beginning of a linked list typically identified and managed?
- A
Via a dedicated 'head' pointer that stores the first node's address.
- B
By storing the first node's index in a global integer variable.
- C
Through a special sentinel node at the end of the list.
- D
By searching for the node with the smallest data value.
Correct answer
AVia a dedicated 'head' pointer that stores the first node's address.
Explanation
A linked list is typically managed by a 'head' or 'list' pointer that always points to the first node in the sequence. The content says: 'the linked list itself, it turns out, we're going to spend one final pointer that just keeps track of where the start of or the so-called head of the list begins in memory'.
- A
- 16
What term is used for a generic container of data in memory within a data structure?
- A
Element
- B
Node
- C
Block
- D
Cell
Correct answer
BNode
Explanation
The term 'node' is commonly used in data structures like linked lists and trees to refer to a generic container that holds data and typically pointers to other nodes. The content introduces: 'we actually introduce a new term of art, node. It doesn't really have a technical definition as much as it is just a container for data in memory'.
- A
- 17
What does the
->(arrow) operator signify in C when used with pointers to structs?- A
It assigns a new memory address to the pointer variable.
- B
It compares the values of two different struct members.
- C
It dereferences a pointer and accesses a member of the struct.
- D
It performs arithmetic addition on the pointer's address.
Correct answer
CIt dereferences a pointer and accesses a member of the struct.
Explanation
The arrow operator
->is syntactic sugar in C that combines dereferencing a pointer (*pointer) with accessing a member (.member) of the struct it points to. The content notes: 'an arrow, which is just a hyphen and a greater than sign, which, as we'll see, is the exact same thing of using a period and a star together'. - A
- 18
What happens if a node in a linked list becomes 'orphaned'?
- A
Its memory cannot be freed, leading to a memory leak.
- B
It automatically gets reattached to the end of the list.
- C
It causes a segmentation fault due to invalid access.
- D
Its memory is immediately reclaimed by the operating system.
Correct answer
AIts memory cannot be freed, leading to a memory leak.
Explanation
If a node in a linked list becomes orphaned (no pointers pointing to it), its memory can no longer be accessed or explicitly freed by the program, resulting in a memory leak. The content states: 'I have orphaned the very first node because no one is pointing at it anymore. And this is actually a really bad memory leak because if you have no variables pointing to that chunk of memory, you literally cannot free it later on in your code.'
- A
- 19
What is the Big O notation for searching an unsorted linked list?
- A
O(log n) - Logarithmic time.
- B
O(n) - Linear time.
- C
O(n^2) - Quadratic time.
- D
O(1) - Constant time.
Correct answer
BO(n) - Linear time.
Explanation
Searching an unsorted linked list requires potentially traversing every node from the beginning to the end in the worst case, leading to a linear time complexity of O(n). The content notes: 'How much time does it take right now to search a linked list from left to right for some value? Big O of n. Why is that? Well, you have to start at the start of the list, the head of the list. And if you're looking for any value, whether it's sorted or not, worst case, you might have to go from left to right all the way to the end'.
- A
- 20
What is the Big O notation for prepending (inserting at the beginning) a new node into a linked list?
- A
O(log n) - Logarithmic time.
- B
O(n log n) - Linearithmic time.
- C
O(1) - Constant time.
- D
O(n) - Linear time.
Correct answer
CO(1) - Constant time.
Explanation
Prepending a node to a linked list involves updating only a few pointers at the head of the list, which takes a constant amount of time regardless of the list's size, hence O(1). The content explains: 'I was adding it to the beginning, which means big O of—it's actually big O of 1 because the beginning of the list is always perfectly accessible to me. It doesn't matter how long the list gets'.
- A
- 21
What is the Big O notation for appending (inserting at the end) a new node to a singly-linked list without a tail pointer?
- A
O(n) - Linear time.
- B
O(n^2) - Quadratic time.
- C
O(1) - Constant time.
- D
O(log n) - Logarithmic time.
Correct answer
AO(n) - Linear time.
Explanation
Appending to a singly-linked list without a tail pointer requires traversing the entire list to find the last node, making it a linear time operation (O(n)). The content says: 'No longer are we inserting in constant time. Now all of our appends require big O of n time, just like our searches, because we have to constantly search from left to right, search from left to right'.
- A
- 22
Why is it unsafe to access a pointer after its memory has been
freed in C?- A
The
freefunction encrypts the memory contents for security. - B
The operating system might reallocate it, making access invalid.
- C
The memory region is immediately overwritten with new data.
- D
The pointer variable itself becomes null, preventing access.
Correct answer
BThe operating system might reallocate it, making access invalid.
Explanation
Once memory is freed, the operating system reclaims it, and the program should no longer touch that memory, as it might be reallocated for other purposes or contain invalid data. The content warns: 'as soon as you have freed an address in memory, you may not touch it again. You can think of it as the operating system immediately might do something with it, and it's not yours to touch anymore'.
- A
- 23
What is the primary advantage of a binary search tree over a sorted array for dynamic data?
- A
Binary search trees do not require data to be stored contiguously.
- B
Binary search trees provide O(1) search time in all scenarios.
- C
Binary search trees eliminate the need for any additional pointers.
- D
Binary search trees use less memory than a simple sorted array.
Correct answer
ABinary search trees do not require data to be stored contiguously.
Explanation
A key advantage of binary search trees is their dynamic nature: they can grow and shrink without the need for contiguous memory, unlike arrays. They use pointers to link nodes, which can be anywhere in memory. The content discusses how linked lists (and by extension trees built with pointers) overcome the contiguous memory requirement. The content discusses how the 'dynamism, the growability of a linked list' is gained back with trees.
- A
- 24
Why can binary search not be directly performed on a singly-linked list?
- A
Binary search requires constant time insertion, which lists lack.
- B
There is no efficient way to jump to the middle of the list.
- C
Singly-linked lists inherently store unsorted data elements.
- D
Pointers in a singly-linked list can only move backward.
Correct answer
BThere is no efficient way to jump to the middle of the list.
Explanation
Binary search requires efficient access to the middle element, which is not possible in a singly-linked list because you can only traverse sequentially from the beginning. The content states: 'the only way to get to the middle is to traverse, frankly, the whole thing, figure out how long it is, then do it again 50% as much time to get to the middle'.
- A
- 25
In a balanced binary search tree, what is the Big O notation for search operations?
- A
O(n log n) - Linearithmic time.
- B
O(n) - Linear time.
- C
O(log n) - Logarithmic time.
- D
O(1) - Constant time.
Correct answer
CO(log n) - Logarithmic time.
Explanation
When a binary search tree is balanced, each search operation effectively halves the remaining search space, resulting in a logarithmic time complexity of O(log n). The content says: 'this is a nice logarithmic data structure' and 'the height of this tree, in other words, is log base 2 of n'.
- A
- 26
What is the primary rule for maintaining the binary search tree property?
- A
Every node must have exactly two children nodes.
- B
Left children are smaller than parent, right children are larger.
- C
The value of a left child is always greater than its parent.
- D
All leaf nodes must be at the same level of the tree.
Correct answer
BLeft children are smaller than parent, right children are larger.
Explanation
The binary search tree property dictates that for any given node, all values in its left subtree are less than its own value, and all values in its right subtree are greater (or equal to) its own value. The content defines this: 'every node's left child is less than the root, and every node's right child is greater than the root'.
- A
- 27
What is the worst-case scenario for a binary search tree if elements are inserted in a sorted order?
- A
It becomes a perfectly balanced tree, optimizing search times.
- B
It requires double the memory for storing redundant pointers.
- C
It degenerates into a singly-linked list, losing logarithmic performance.
- D
It automatically self-corrects to maintain its balanced structure.
Correct answer
CIt degenerates into a singly-linked list, losing logarithmic performance.
Explanation
If elements are inserted into a binary search tree in a strictly sorted (or reverse sorted) order, it degenerates into a linear structure resembling a linked list, thus losing its logarithmic time advantage. The content demonstrates this with numbers 1, 2, 3, 4, 5, 6, asking: 'What is my binary search tree effectively devolving into? A linked list.'
- A
- 28
What is a dictionary, as an abstract data type, primarily used for?
- A
Associating unique keys with corresponding values in storage.
- B
Optimizing memory allocation for fixed-size data records.
- C
Maintaining a collection of items with FIFO or LIFO properties.
- D
Storing a list of elements in a specific sequential order.
Correct answer
AAssociating unique keys with corresponding values in storage.
Explanation
A dictionary, also known as a map or associative array, is an abstract data type that stores a collection of key-value pairs, allowing retrieval of a value based on its associated key. The content states: 'A dictionary lets you do that—associate keys with values'.
- A
- 29
What is the main goal when designing a hash function for a hash table?
- A
To minimize the total memory usage of the underlying array.
- B
To ensure all input values produce the same hash output.
- C
To uniformly distribute inputs across the available buckets.
- D
To guarantee that the hash table always maintains sorted order.
Correct answer
CTo uniformly distribute inputs across the available buckets.
Explanation
A good hash function aims to distribute input data as evenly as possible across the hash table's buckets to minimize collisions and maintain efficient access times. The content discusses: 'bucketization helps take an infinite, or a very large domain, and map it to a smaller range of value'.
- A
- 30
What is a 'collision' in the context of a hash table?
- A
When two identical inputs produce different hash values.
- B
When a hash function fails to generate any output value.
- C
When the hash table runs out of available memory space.
- D
When two different inputs produce the same hash value.
Correct answer
DWhen two different inputs produce the same hash value.
Explanation
A collision occurs when a hash function maps two different input keys to the same index (bucket) in the hash table. The content states: 'what's going to happen as soon as I have two characters whose names start with the same letter in the Nintendo universe? This is eventually surely going to happen. So far, I've gotten lucky with all of these other characters. But I do think, indeed, before long, I'm going to have what we're going to call collisions, whereby here, two L names somehow collide.'
- A
- 31
How does a hash table combine arrays and linked lists to handle collisions?
- A
It uses a fixed-size array for values and a linked list for keys.
- B
It uses a single large array where each element points to another array.
- C
It stores all data in a single linked list, indexed by hash value.
- D
It is an array where each bucket is the head of a linked list.
Correct answer
DIt is an array where each bucket is the head of a linked list.
Explanation
A common implementation of a hash table uses an array of linked lists, where each array index (bucket) can store a linked list of elements that hash to that same index. The content explicitly states: 'I could kind of link all of the similarly-named names together using a linked list here. And so this is why I say hash table is an array of linked lists'.
- A
- 32
What is the theoretical worst-case Big O time complexity for searching a hash table?
- A
O(n^2) - Quadratic time.
- B
O(1) - Constant time.
- C
O(log n) - Logarithmic time.
- D
O(n) - Linear time.
Correct answer
DO(n) - Linear time.
Explanation
In the worst-case scenario, if all elements hash to the same bucket, a hash table degenerates into a single linked list, and searching would take linear time, O(n). The content notes: 'in the most perverse case, all n names are in the same bucket and the linked list at that location. So who really cares that you're calling it a hash table? You're not using 25, for instance, of the locations. But that's a theoretical risk.'
- A
- 33
What is the practical (average-case) time complexity for searching a well-designed hash table?
- A
O(n) - Linear time, as it traverses a linked list.
- B
O(log n) - Logarithmic time, due to tree-like structure.
- C
O(n^2) - Quadratic time, because of nested loops.
- D
O(1) - Constant time, very close to ideal.
Correct answer
DO(1) - Constant time, very close to ideal.
Explanation
With a good hash function that distributes elements uniformly, the average-case search time for a hash table is effectively constant time, O(1), even if theoretically it's O(n/k). The content says: 'most of the time, it's going to be indeed constant time, a.k.a. big O of 1'.
- A
- 34
What is a trie data structure primarily used for?
- A
Storing elements in a fixed-size, contiguous memory block.
- B
Efficiently storing and retrieving sequences of numbers.
- C
Implementing dictionaries with constant time string lookups.
- D
Managing a chronological order of events or tasks.
Correct answer
CImplementing dictionaries with constant time string lookups.
Explanation
A trie (prefix tree) is a specialized tree-like data structure used for efficient retrieval of keys, especially strings, by storing them based on their prefixes. It allows for constant-time lookups based on string length. The content states: 'trie just means it's a data structure for retrieving data. It is an alternative data structure via which, again, we can implement dictionaries, key value pairs.'
- A
- 35
What can a trie data structure be conceptually described as?
- A
A linked list where each node stores a full string.
- B
A simple array of characters with a null terminator.
- C
A hash table with a single, very large bucket for all data.
- D
A tree of arrays, where each array represents a letter.
Correct answer
DA tree of arrays, where each array represents a letter.
Explanation
A trie is essentially a tree-like structure where each node is an array of pointers, with each pointer representing a letter in the alphabet, linking to another array for the next letter in a word. The content describes: 'a trie is a tree of arrays you can think of it as'.
- A
- 36
What is the Big O time complexity for searching a trie for a given string?
- A
O(1) - Constant time, independent of total elements.
- B
O(L) - Dependent on the length (L) of the search string.
- C
O(log n) - Logarithmic time, depending on tree height.
- D
O(n) - Linear time, based on total number of elements.
Correct answer
BO(L) - Dependent on the length (L) of the search string.
Explanation
The search time in a trie depends directly on the length of the key (string) being searched, not the total number of elements in the trie, making it O(L). The content specifies: 'It would seem to depend only on the length of the person's name'.
- A
- 37
What is the primary trade-off associated with using a trie data structure?
- A
It cannot dynamically grow or shrink based on data requirements.
- B
It requires a significant amount of memory for its array-based nodes.
- C
It is difficult to implement due to complex pointer manipulations.
- D
It suffers from frequent collisions that slow down access.
Correct answer
BIt requires a significant amount of memory for its array-based nodes.
Explanation
Tries offer very fast lookup times but at the cost of consuming a large amount of memory, especially if the alphabet size is large and many branches of the tree are unused. The content asks: 'What's the trade then for a trie? Space? What Space? ... The space to store all of those arrays'.
- A
- 38
Which real-world example best represents a hash table implementation for quick retrieval?
- A
Sweetgreen shelves categorizing salads by the first letter of a name.
- B
A folder of physical documents organized chronologically.
- C
A library catalog sorted by book title and author.
- D
A grocery store checkout line with customers waiting.
Correct answer
ASweetgreen shelves categorizing salads by the first letter of a name.
Explanation
The Sweetgreen example illustrates a hash table where salads are 'hashed' (categorized) into specific shelf locations based on the first letter of the customer's name for quick retrieval. The content directly explains: 'This is effectively a hash table. Why? Because when someone has a salad with their name on it...they're going to put it in a specific location'.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.