C Programming Data Structures
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is the primary constraint of an array data structure?
Arrays must be of a generally uniform data type and their size must be decided in advance.
Or swipe the card
44 cards
What is the primary constraint of an array data structure?
Arrays must be of a generally uniform data type and their size must be decided in advance.
What happens if an array needs to store more data than its initial allocated size?
To grow an array, you typically have to copy all existing elements to a brand new, larger contiguous memory location and then add the new data.
What is the downside of over-allocating memory for an array to avoid resizing?
Over-allocating memory leads to wasted space, as the unused portion of the allocated memory cannot be used by other programs or parts of the current program.
What is
reallocin C, and how does it simplify array resizing?reallocis a C function that attempts to resize an already allocated block of memory. It can extend the existing block if space is available or allocate a new, larger block and copy the contents, returning the address of the new block.What does
NULLsignify in the context of memory allocation in C?NULLindicates that a memory allocation function (likemallocorrealloc) failed to allocate the requested memory, usually because no sufficient memory block was available.What is a 'memory leak' in C programming?
A memory leak occurs when a program allocates memory dynamically but fails to free it when it's no longer needed, causing the program's memory footprint to grow unnecessarily.
What is the primary trade-off often encountered in computer science when solving problems?
The primary trade-off is often between time (how fast an operation runs) and space (how much memory an operation or data structure uses).
What is an Abstract Data Type (ADT)?
An Abstract Data Type (ADT) is a logical description of a data structure that specifies its behavior, operations, and properties without regard to its underlying implementation details.
Provide a real-world example of a queue.
A line of people waiting for a movie ticket or for dinner is a real-world example of a queue.
What principle governs the behavior of a queue?
A queue operates on the FIFO principle: First In, First Out.
What are the two fundamental operations associated with a queue?
The two fundamental operations are enqueue (to add an element to the queue) and dequeue (to remove an element from the queue).
What is the difference between 'capacity' and 'size' when implementing a queue in code?
Capacity refers to the maximum number of elements the queue can hold, typically a constant. Size refers to the current number of elements actively stored in the queue, which changes dynamically.
Provide a real-world example of a stack.
A stack of plates in a dining hall or clothes in a drawer are real-world examples of a stack.
What principle governs the behavior of a stack?
A stack operates on the LIFO principle: Last In, First Out.
What are the two fundamental operations associated with a stack?
The two fundamental operations are push (to add an element to the top of the stack) and pop (to remove an element from the top of the stack).
What is a
structin C used for?A
struct(structure) in C is a user-defined data type that groups related variables of different data types under a single name.What does the
->(arrow) operator do in C when used with pointers to structures?The
->(arrow) operator is used to access members of a structure or union via a pointer to that structure or union.What is a 'singly-linked list'?
A singly-linked list is a linear data structure where each element (node) contains two parts: the data itself and a pointer (or reference) to the next node in the sequence.
What are the advantages of a linked list over an array for storing dynamic data?
Linked lists are dynamically sized, allowing them to grow or shrink as needed without reallocating and copying all elements. They can also make more efficient use of memory by placing nodes non-contiguously.
What is the primary downside of a linked list compared to an array?
Linked lists require more memory due to the overhead of storing pointers in each node, and they do not allow for efficient random access (e.g., accessing the k-th element directly).
How do you represent the end of a linked list?
The
nextpointer of the last node in a linked list is set toNULLto signify the end of the list.What is the time complexity for searching an unsorted singly-linked list?
The time complexity for searching an unsorted singly-linked list is O(n) (linear time), because in the worst case, you might have to traverse every node.
What is the time complexity for inserting a new node at the beginning of a singly-linked list?
Inserting a new node at the beginning (prepending) of a singly-linked list takes O(1) (constant time).
What is the time complexity for inserting a new node at the end of a singly-linked list (without a tail pointer)?
Inserting a new node at the end (appending) of a singly-linked list typically takes O(n) (linear time) because you must traverse the entire list to find the last node.
Why is 'orphaning' a node a problem in C when dealing with dynamically allocated memory?
Orphaning a node means losing the pointer to a dynamically allocated memory block. This leads to a memory leak because the program can no longer
freethat memory, making it permanently inaccessible until the program terminates.Explain the logic for freeing all nodes in a singly-linked list.
To free a singly-linked list, you must iterate through each node. Before freeing the current node, save its
nextpointer to a temporary variable so you can advance to the next node after freeing the current one.What is a 'doubly-linked list'?
A doubly-linked list is a linear data structure where each node has pointers to both the next node and the previous node in the sequence.
What is a 'tree' data structure?
A tree is a non-linear, hierarchical data structure that consists of nodes connected by edges, resembling an upside-down family tree. It has a single 'root' node at the top, and 'children' nodes branching downwards.
What is a 'binary search tree' (BST)?
A binary search tree is a type of tree data structure where each node has at most two children, referred to as the 'left' child and 'right' child. It maintains the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater than (or equal to) the node's value.
What are the components of a node in a binary search tree?
A node in a binary search tree typically contains the data itself (e.g., an integer
number), a pointer to itsleftchild (another node), and a pointer to itsrightchild (another node).What is the time complexity for searching, inserting, or deleting an element in a balanced binary search tree?
In a balanced binary search tree, these operations have a time complexity of O(logn) (logarithmic time), where n is the number of nodes.
What is the worst-case scenario for a binary search tree, and what is its time complexity?
In the worst case, if elements are inserted in a strictly ascending or descending order, a binary search tree can devolve into a linked list. In this scenario, searching, insertion, and deletion operations become O(n) (linear time).
What is a 'dictionary' as an Abstract Data Type (ADT)?
A dictionary (also known as a map or associative array) is an ADT that stores collections of (key, value) pairs, allowing values to be retrieved efficiently using their associated keys.
What is 'hashing' in computer science?
Hashing is the process of converting an input (or 'key') into a fixed-size value (a 'hash value' or 'hash code') using a hash function. This hash value is typically used as an index into an array or 'bucket'.
What is a 'hash table'?
A hash table is a data structure that implements a dictionary (key-value store) by using a hash function to map keys to indices in an array of buckets. Each bucket typically stores a linked list of key-value pairs.
What is a 'collision' in the context of hash tables?
A collision occurs when a hash function maps two or more different keys to the same index (or bucket) in a hash table.
What is the ideal (best-case) time complexity for searching, inserting, or deleting an element in a hash table?
In the best case (with a perfectly uniform hash function and no collisions), these operations can achieve O(1) (constant time).
What is the worst-case time complexity for searching, inserting, or deleting an element in a hash table?
In the worst case (if all keys hash to the same bucket, leading to many collisions), these operations can degrade to O(n) (linear time), effectively reducing the hash table to a single linked list.
What is the 'birthday problem' in statistics, and how does it relate to hash collisions?
The birthday problem demonstrates that in a relatively small group of people, there's a surprisingly high probability that two people will share the same birthday. It relates to hash collisions by illustrating that even with a good hash function, collisions are highly probable in hash tables even with a relatively small number of inputs.
What is a 'trie' data structure, and what is its alternative name?
A trie (pronounced 'try') is a tree-like data structure that stores a dynamic set of strings, often used for retrieval. It is also known as a prefix tree.
How is a trie typically structured internally?
A trie can be thought of as a 'tree of arrays'. Each node is an array of pointers (or references), where each index in the array corresponds to a character (e.g., A-Z). These pointers lead to child nodes, which are themselves arrays.
What is the time complexity for searching, inserting, or deleting a string in a trie?
These operations in a trie have a time complexity of O(L), where L is the length of the key (string). This is considered O(1) (constant time) because the maximum length of a key is typically bounded.
What is the primary disadvantage of using a trie data structure?
The primary disadvantage of a trie is its high memory consumption (space complexity), especially if the alphabet is large or the stored words are very long and diverse.
How can real-world scenarios, like a Sweetgreen salad pickup, be viewed as a hash table?
A Sweetgreen salad pickup shelf acts as a hash table: salads (values) are placed into specific letter-based sections (buckets or indices) based on the first letter of the customer's name (key).

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