๐ŸŒ CS50x: Introduction to Computer Science ยท subject

CS50x: Introduction to Computer Science Data Structures Syllabus

The Data Structures syllabus for CS50x: Introduction to Computer Science in full: 4 chapters and 9 topics, every one listed below, plus 50 Data Structures flashcards with answers.

4Chapters
9Topics
50Flashcards

Data Structures syllabus: all 4 chapters

Expand any chapter to see its topics and sub-topics.

  1. Abstract Data Types

    3 topics
    • Structures
    • Stacks
    • Queues
  2. Linked Lists

    2 topics
    • Singly-Linked Lists
    • Doubly-Linked Lists
  3. Trees

    2 topics
    • Binary Search Trees
    • Tries
  4. Hash Tables

    2 topics
    • Hash Functions
    • Collisions and Chaining

CS50x: Introduction to Computer Science Data Structures flashcards โ€” 50 questions with answers

  1. In C, what is a structure (struct)?

    A composite (user-defined) data type that groups multiple related variables, possibly of different types, together under one name so they can be treated as a single unit (e.g., a person with a name and a phone number).

  2. Write the typical C syntax (as used in CS50) to define a structure type called person with a name and a number field.

    typedef struct { string name; string number; } person; โ€” typedef gives the structure a usable type name so you can declare variables like person p;

  3. How do you access a field of a struct variable versus a field of a struct accessed through a pointer in C?

    Use the dot operator for a variable: p.name. Use the arrow operator for a pointer: ptr->name, which is shorthand for (*ptr).name (dereference, then access the field).

  4. How do you dynamically allocate memory for one struct node in C, and what must you check afterward?

    node *n = malloc(sizeof(node)); You must check whether n == NULL (malloc failed) before using it, and later call free(n) to avoid a memory leak.

  5. Why are structures a prerequisite for building linked lists, trees, and tries?

    Because each element (node) must bundle data together with one or more pointers to other nodes; a struct is the mechanism that groups a value and its pointer(s) into a single self-referential type.

  6. What is a stack, and what ordering principle does it follow?

    A stack is a data structure in which elements are added and removed only from the top. It follows LIFO โ€” Last In, First Out: the most recently added element is the first one removed.

  7. Name and define the two fundamental operations on a stack.

    Push: add an element onto the top of the stack. Pop: remove (and return) the element from the top of the stack.

  8. What is the time complexity of push and pop on a stack?

    Both are $O(1)$ โ€” constant time, because they only touch the top of the stack regardless of how many elements it holds.

  9. Give two real-world or computing examples that behave like a stack.

    A stack of cafeteria trays (you take the top one, the last one placed); the undo feature in an editor; the function call stack, where the most recently called function returns first.

  10. In an array-based stack of fixed capacity, when do overflow and underflow occur?

    Overflow: pushing when the stack is already full (size equals capacity). Underflow: popping when the stack is empty (size is $0$). Both must be guarded against.

  11. What is a queue, and what ordering principle does it follow?

    A queue is a data structure where elements are added at the back and removed from the front. It follows FIFO โ€” First In, First Out: the earliest added element is the first one removed.

  12. Name and define the two fundamental operations on a queue.

    Enqueue: add an element to the end (back/tail) of the queue. Dequeue: remove the element from the front (head) of the queue.

  13. What is the time complexity of enqueue and dequeue on a properly implemented queue?

    Both are $O(1)$ โ€” constant time, achieved by keeping references (or indices) to both the front and the back of the queue.

  14. How do a stack and a queue differ in behavior?

    A stack is LIFO: insertions and removals happen at the same end (the top), so the newest element leaves first. A queue is FIFO: insertions happen at the back and removals at the front, so the oldest element leaves first โ€” fair like a line of people.

  15. In an array-based queue, why is the array typically treated as circular, and what extra bookkeeping is needed?

    So that dequeuing from the front does not waste slots or require shifting all elements: front and back indices wrap around using modular arithmetic (index $\bmod$ capacity). You track the front index and the current size.

  16. What two fields does a node of a singly-linked list contain (in CS50-style C)?

    A value field (the data, e.g., int number) and a pointer to the next node: struct node *next. The struct must use its full name internally because it refers to itself.

  17. What is the time complexity of inserting a new element at the head of a singly-linked list, and why?

    $O(1)$ โ€” you allocate a node, point its next at the current head, and update the head pointer; no traversal or shifting is needed regardless of list length.

  18. What is the time complexity of searching a singly-linked list of $n$ elements, and why can't you use binary search on it?

    $O(n)$, because you must follow next pointers one node at a time from the head. Binary search is impossible since a linked list has no random access โ€” you cannot jump to the middle element in constant time.

  19. When inserting a new node at the head of a linked list, why must you set the new node's next pointer before updating the head pointer?

    If you update head first, you lose your only reference to the rest of the list โ€” the remaining nodes become unreachable (orphaned/leaked). Always link the new node into the list, then move head.

  20. How is the end of a linked list marked, and how does a traversal loop use this?

    The last node's next pointer is set to NULL. Traversal runs a cursor: for (node *tmp = list; tmp != NULL; tmp = tmp->next), stopping when the cursor reaches NULL.

Show the other 30 Data Structures flashcards
  1. State the main trade-offs of a linked list compared to an array.

    Linked list: dynamic size, $O(1)$ insertion at the head, no need to copy/resize โ€” but $O(n)$ search, no random indexing, and extra memory for pointers. Array: $O(1)$ random access and binary-search-friendly if sorted ($O(\log n)$) โ€” but fixed size and $O(n)$ insertion (shifting or reallocating).

  2. How does a doubly-linked list node differ from a singly-linked list node?

    Each node stores two pointers instead of one: next (to the following node) and prev (to the preceding node), plus the value. This allows traversal in both directions.

  3. What operation becomes $O(1)$ in a doubly-linked list (given a pointer to the node) but requires extra work in a singly-linked list, and why?

    Deleting a given node. With a prev pointer you can relink neighbors directly: node->prev->next = node->next and node->next->prev = node->prev. In a singly-linked list you must first traverse from the head to find the predecessor, costing $O(n)$.

  4. What is the main cost of a doubly-linked list compared to a singly-linked list?

    Extra memory: every node stores an additional prev pointer, and every insertion/deletion must correctly update twice as many pointers, increasing code complexity and room for bugs.

  5. When freeing an entire linked list in C, what order must operations follow to avoid losing nodes?

    Save a pointer to the next node before freeing the current one: while (list != NULL) { node *tmp = list->next; free(list); list = tmp; }. Freeing first would make list->next invalid. Tools like valgrind verify no memory is leaked.

  6. State the binary search tree (BST) property.

    For every node: all values in its left subtree are smaller than the node's value, and all values in its right subtree are larger. This property holds recursively for every subtree.

  7. What three fields does a BST node contain?

    A value, a pointer to the left child (struct node *left), and a pointer to the right child (struct node *right); absent children are NULL.

  8. Describe the recursive procedure for searching a BST for a value $x$.

    If the current node is NULL, return false (not found). If $x$ equals the node's value, return true. If $x$ is less than the value, recursively search the left subtree; if greater, recursively search the right subtree โ€” discarding half the remaining tree at each step (when balanced).

  9. What is the search time complexity of a balanced BST with $n$ nodes, and why?

    $O(\log n)$, because a balanced tree of $n$ nodes has height about $\log_{2} n$, and each comparison descends one level, halving the search space.

  10. What is the worst-case shape of a BST, when does it arise, and what is search time then?

    A degenerate (unbalanced) tree that is effectively a linked list โ€” e.g., when keys are inserted in already-sorted order, every node has only one child. Search degrades to $O(n)$.

  11. Which traversal of a BST visits the values in sorted (ascending) order, and what is its recursive pattern?

    In-order traversal: recursively visit the left subtree, then the current node, then the right subtree. The BST property guarantees this yields the values in ascending order.

  12. Compare a BST to a sorted array for the combination of searching and inserting.

    Both search in $O(\log n)$ (balanced BST vs. binary search on the array). But insertion into a balanced BST is $O(\log n)$, while inserting into a sorted array is $O(n)$ because elements must shift; the BST keeps data dynamic without wholesale copying.

  13. What is a trie, and where does its name come from?

    A trie is a tree in which each key is stored implicitly as a path of characters from the root, one character per level โ€” a tree of arrays of pointers. The name comes from 'retrieval' (pronounced 'try').

  14. In a CS50-style trie storing lowercase English words, what does each node contain?

    An array of $26$ pointers to child nodes (one per letter $a$โ€“$z$) and a boolean flag such as is_word marking whether the path from the root to this node spells a complete stored word.

  15. What is the lookup time in a trie for a key of length $k$ stored among $n$ keys?

    $O(k)$ โ€” you follow one pointer per character of the key. Crucially this is independent of $n$: lookup takes the same time whether the trie holds ten words or ten million, effectively constant time for bounded-length keys such as names.

  16. What is the main disadvantage of a trie?

    Enormous memory usage: every node allocates an entire array of child pointers (e.g., $26$ per node for letters), most of which are NULL, so tries trade large amounts of space for very fast lookups.

  17. How do you insert the word 'cat' into an (initially empty) lowercase-letter trie?

    Starting at the root, follow (creating if NULL) the child pointer for 'c', then from that node for 'a', then for 't'; at the final node set the is_word flag to true. Shared prefixes (like 'car' and 'cat') reuse the same initial path.

  18. What is a hash function?

    A function that takes input data (e.g., a string key) and deterministically returns a nonnegative integer โ€” the hash code โ€” used as an index (bucket number) into a hash table's array.

  19. Name three properties a good hash function for a hash table should have.

    (1) Deterministic: the same input always yields the same index. (2) Uniform: it spreads keys evenly across buckets to minimize collisions. (3) It outputs only valid indices (e.g., via taking the value $\bmod$ the array size) and is fast to compute.

  20. What is a hash table, structurally?

    The combination of an array (the buckets) and a hash function that maps each key to an array index; conceptually, an array of $n$ buckets where element placement is decided by hashing โ€” in CS50, typically an array of linked lists.

  21. What is the ideal (average-case) lookup time of a hash table, and its worst case?

    Ideal/average: $O(1)$ โ€” hash the key and go straight to its bucket. Worst case: $O(n)$, when all $n$ keys collide into a single bucket and you must scan a linked list of length $n$.

  22. Give the classic simple example of a hash function for names, and its main weakness.

    Hash on the first letter: index $=$ first character $-$ 'A', giving $26$ buckets (like sorting cards into piles). Weakness: highly non-uniform โ€” many names share first letters (and letters like Q or X are rare), producing many collisions and unbalanced buckets.

  23. What is a collision in a hash table?

    The event where two different keys hash to the same array index (bucket), so they compete for the same location and the table needs a resolution strategy to store both.

  24. Why are collisions unavoidable in general for a hash table?

    By the pigeonhole principle: the number of possible keys is (much) larger than the number of buckets $m$, so once more than $m$ distinct keys are possible, some two keys must map to the same bucket no matter how good the hash function is.

  25. What is chaining (separate chaining) as a collision-resolution strategy?

    Each bucket of the hash table holds a pointer to a linked list; all keys that hash to that index are inserted into that bucket's list (typically prepended in $O(1)$). Lookup hashes to the bucket, then linearly searches its chain.

  26. With chaining, if $n$ keys are distributed evenly over $k$ buckets, how long is an average lookup, and what is this asymptotically?

    About $\frac{n}{k}$ comparisons on average (the load factor). Since $k$ is a constant, this is still $O(n)$ asymptotically โ€” but in practice a good hash function with enough buckets makes lookups roughly $k$ times faster, approaching constant time.

  27. What is linear probing, and what problem can it suffer from?

    An open-addressing alternative to chaining: on a collision, step forward through the array (index $+1, +2, \dots$, wrapping around) until an empty slot is found. It suffers from clustering โ€” runs of occupied slots grow and lengthen future probes โ€” and the table can fill up entirely.

  28. Rank these structures by lookup cost for $n$ stored items: unsorted linked list, sorted array (binary search), hash table (average), trie (key length $k$).

    Trie: $O(k)$ (constant in $n$) $\approx$ hash table average: $O(1)$; then sorted array: $O(\log n)$; slowest is the unsorted linked list: $O(n)$. (Hash table worst case degrades to $O(n)$; trie pays with heavy memory use.)

  29. Which data structure in CS50 offers lookup time independent of the number of stored elements, and at what cost?

    The trie: lookup is $O(k)$, where $k$ is the key's length, regardless of how many keys are stored. The cost is very large memory consumption, since each node holds a full array of mostly-NULL child pointers.

  30. Summarize the core trade-off theme of CS50's data structures lecture: what do you generally exchange when choosing a structure?

    Time versus space (and code complexity): arrays are memory-lean with fast random access but costly to grow; linked lists grow in $O(1)$ but search in $O(n)$; balanced BSTs give $O(\log n)$ dynamic operations; hash tables approach $O(1)$ average lookups; tries achieve $O(k)$ lookups by spending huge amounts of memory.

CS50x: Introduction to Computer Science Data Structures FAQ

What is in the CS50x: Introduction to Computer Science Data Structures syllabus?

CS50x: Introduction to Computer Science Data Structures is split into 4 chapters: Abstract Data Types, Linked Lists, Trees and Hash Tables. Together they contain 9 topics.

How many chapters are in CS50x: Introduction to Computer Science Data Structures?

4 chapters, containing 9 topics. The largest chapter is Abstract Data Types, with 3 topics.

Are there flashcards for CS50x: Introduction to Computer Science Data Structures?

Yes โ€” a 50-card Data Structures deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.