1. Home
  2. Explore
  3. Computer Science
  4. CS50 Data Structures Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CS50 Data Structures Overview

  • 31Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Abstract Data Types

What is an abstract data type (ADT)?

31 questions

  1. 1

    What is an abstract data type (ADT)?

    Model answer

    An ADT is a high-level description of a data structure that specifies certain properties and characteristics, leaving the implementation details to the programmer.

    Explanation

    The lecture introduces ADTs like queues and stacks, noting that they define behavior (e.g., FIFO, LIFO) but not how they are implemented in code.

  2. 2

    Define the FIFO property and name the two operations associated with a queue.

    Model answer

    FIFO stands for First In, First Out. The operations are enqueue (add to back) and dequeue (remove from front).

    Explanation

    The lecture demonstrates FIFO with volunteers and explicitly names enqueue and dequeue as queue operations.

  3. 3

    How does a stack differ from a queue in terms of ordering and operations?

    Model answer

    A stack uses LIFO (Last In, First Out) ordering, with operations push (add to top) and pop (remove from top).

    Explanation

    The lecture contrasts LIFO with FIFO and uses the examples of volunteers stacked against a lectern and Gmail inbox.

  4. 4

    When implementing a queue or stack using an array, what additional integer is needed, and why?

    Model answer

    An integer to track the number of elements currently in the data structure (size), so you know how much capacity is used and remaining.

    Explanation

    The lecture shows that a queue structure contains an array and a size field to know how many people are in the queue.

  5. 5

    Why is the array approach problematic for a queue or stack in terms of capacity?

    Model answer

    Arrays have a fixed size; if more elements are added than capacity, no further elements can be stored, even if memory is available elsewhere.

    Explanation

    The lecture notes that with an array of capacity 50, a 51st person cannot be added, showing the limitation of fixed-size arrays.

  6. 6

    What is a key characteristic of arrays in memory?

    Model answer

    Arrays store data contiguously in memory, meaning elements are placed one after another without gaps.

    Explanation

    The lecture reviews that arrays are contiguous, which is why adding elements beyond the allocated space is problematic.

  7. 7

    Explain why you cannot simply place a fourth number next to an existing array of three numbers if the adjacent memory is already occupied.

    Model answer

    Arrays require contiguous memory; if the memory right after the array is used by another variable, you cannot extend the array there.

    Explanation

    The lecture uses the example of a string 'Hello, world!' stored next to the array to illustrate this constraint.

  8. 8

    What are two major costs of reallocating a larger array to accommodate more elements?

    Model answer

    Time (copying all elements from old to new array takes O(n) steps) and memory (temporarily using both old and new arrays).

    Explanation

    The lecture discusses that iterating through the array to copy and using twice the memory are downsides.

  9. 9

    What function is used to dynamically allocate memory in C, and what header file is required?

    Model answer

    malloc() from <stdlib.h>.

    Explanation

    In the code example, the lecturer includes <stdlib.h> and uses malloc to allocate memory for the list.

  10. 10

    What does malloc return if it fails?

    Model answer

    It returns NULL (the address 0).

    Explanation

    The lecture checks for NULL after malloc and explains that NULL indicates failure.

  11. 11

    In the code snippet, why is it important to free the original list before returning early when the new allocation fails?

    Model answer

    To avoid a memory leak, because the original memory is still allocated but no longer accessible.

    Explanation

    The lecturer notes that returning immediately would leak the originally allocated memory, so free(list) should be called first.

  12. 12

    What is a linked list, and how does it avoid the fixed-size problem of arrays?

    Model answer

    A linked list is a data structure where each node contains data and a pointer to the next node, allowing dynamic growth by allocating nodes as needed.

    Explanation

    The lecture defines linked lists and shows they can grow by allocating new nodes anywhere in memory, using pointers to connect them.

  13. 13

    What are the two fields in a typical linked list node for storing integers?

    Model answer

    An integer (number) and a pointer to the next node (next).

    Explanation

    The struct definition includes 'int number' and 'struct node *next'.

  14. 14

    Why must the struct definition for a node use 'struct node *next' instead of just 'node *next'?

    Model answer

    Because the typedef for 'node' hasn't been completed yet; you must use the full 'struct node' before the typedef finishes.

    Explanation

    The lecture explains that C reads top-to-bottom, so 'node' is not yet defined when the pointer is declared, hence 'struct node *next'.

  15. 15

    What is the running time (Big O) of inserting a node at the beginning of a linked list (prepending)?

    Model answer

    O(1) – constant time, independent of list size.

    Explanation

    The lecture notes that prepending only requires updating a few pointers, regardless of how many nodes already exist.

  16. 16

    What is the running time of searching for an element in a linked list?

    Model answer

    O(n) – linear time, because you may have to traverse the entire list.

    Explanation

    The lecture compares linked list search to array search, noting that you cannot do binary search because memory is not contiguous.

  17. 17

    In the appended-insertion code, why does insertion become O(n) instead of O(1)?

    Model answer

    You must traverse the entire list to find the last node before appending.

    Explanation

    The lecturer walks through the code for appending, showing a loop that goes to the end, taking n steps for n nodes.

  18. 18

    What is the definition of a binary search tree (BST)?

    Model answer

    A BST is a binary tree where for each node, all values in its left subtree are smaller, and all values in its right subtree are larger.

    Explanation

    The lecture gives the rule and illustrates with examples like node 4 with left smaller and right larger.

  19. 19

    In a BST, what are the left and right pointers of a leaf node set to?

    Model answer

    NULL.

    Explanation

    The lecture mentions that leaf nodes have nothing to point to and pointers can be NULL.

  20. 20

    How does the recursive search function for a BST decide whether to go left or right?

    Model answer

    It compares the target number to the current node's number; if target is less, search left subtree; if greater, search right subtree; if equal, return true.

    Explanation

    The lecture shows the code with if-else conditions to go left or right recursively.

  21. 21

    What is the worst-case running time for searching in a BST if the tree becomes unbalanced?

    Model answer

    O(n) – it degenerates to a linked list.

    Explanation

    The lecture demonstrates that inserting numbers in sorted order (1,2,3) creates a chain, not a balanced tree.

  22. 22

    What is a dictionary in computing?

    Model answer

    A dictionary is an abstract data type that stores key-value pairs.

    Explanation

    The lecture defines a dictionary as associating keys (e.g., words) with values (e.g., definitions).

  23. 23

    What is a hash function?

    Model answer

    A function that maps input values (e.g., from an infinite domain) to a finite range of output values (hash values).

    Explanation

    The lecture explains hashing using the example of sorting cards into buckets by suit.

  24. 24

    How is a hash table typically implemented?

    Model answer

    As an array of linked lists; the array provides direct access by hash value, and linked lists handle collisions.

    Explanation

    The lecture shows a diagram with an array of 26 letters, each pointing to a linked list of names starting with that letter.

  25. 25

    What is a collision in the context of hash tables?

    Model answer

    When two different keys hash to the same array index.

    Explanation

    The lecture gives examples like Luigi and Lakitu both mapping to the 'L' bucket.

  26. 26

    In hash functions, what do the keywords 'const' and 'unsigned' indicate in C?

    Model answer

    'const' means the function promises not to modify the input string; 'unsigned' means the returned integer will be non-negative.

    Explanation

    The lecture shows a hash function prototype with 'const char *word' and 'unsigned int' return type.

  27. 27

    What is the theoretical worst-case running time of searching in a hash table?

    Model answer

    O(n) – when all keys collide into one linked list.

    Explanation

    The lecture notes that if all names start with the same letter, the hash table degenerates to a linked list.

  28. 28

    What is a trie (prefix tree)?

    Model answer

    A tree where each node is an array of pointers (often 26 for alphabet), and each path from root to leaf represents a string.

    Explanation

    The lecture illustrates a trie with nodes representing letters, e.g., for 'Toad', 'Toadette', 'Tom'.

  29. 29

    What is the running time for searching in a trie?

    Model answer

    O(k) where k is the length of the word; effectively O(1) since word length is bounded by a constant.

    Explanation

    The lecture argues that word length has a maximum, making search constant time in practice.

  30. 30

    What is the major disadvantage of using a trie?

    Model answer

    It consumes huge amounts of memory because many pointers in arrays remain NULL.

    Explanation

    The lecture points out that even for three names, many array slots are unused, and memory usage explodes for larger alphabets.

  31. 31

    How does the Sweetgreen salad shelf example illustrate a hash table?

    Model answer

    Salads are placed on shelves labeled by first letter of customer's name, allowing O(1) lookup, but collisions occur when many names share the same first letter.

    Explanation

    The lecture uses this real-world example to explain hashing and collisions.

1 of 31
Question 1 of 31

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent