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

  • 28Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Queues and Stacks

What property does a queue exhibit?

28 questions

  1. 1

    What property does a queue exhibit?

    1. A

      Last in, first out (LIFO)

    2. B

      Last in, last out (LILO)

    3. C

      First in, last out (FILO)

    4. D

      First in, first out (FIFO)

    Correct answer
    D

    First in, first out (FIFO)

    Explanation

    A queue follows the FIFO (first in, first out) principle, where the first element added is the first one removed.

  2. 2

    Which operations are associated with a queue?

    1. A

      Add and remove

    2. B

      Insert and delete

    3. C

      Push and pop

    4. D

      Enqueue and dequeue

    Correct answer
    D

    Enqueue and dequeue

    Explanation

    Queue operations are called enqueue (add to back) and dequeue (remove from front). Push and pop are for stacks.

  3. 3

    A stack follows which principle?

    1. A

      First in, first out (FIFO)

    2. B

      First in, last out (FILO)

    3. C

      Last in, first out (LIFO)

    4. D

      Random access

    Correct answer
    C

    Last in, first out (LIFO)

    Explanation

    A stack follows LIFO (last in, first out), where the most recently added element is removed first.

  4. 4

    In which real-world scenario is a stack commonly used?

    1. A

      A list of contacts sorted alphabetically

    2. B

      A line of customers at a store

    3. C

      An email inbox showing newest messages first

    4. D

      A playlist shuffled randomly

    Correct answer
    C

    An email inbox showing newest messages first

    Explanation

    Email inboxes often display newest emails at the top, behaving like a stack where the last email received is seen first (LIFO).

  5. 5

    What is a key characteristic of an array in memory?

    1. A

      Size can grow dynamically without reallocation

    2. B

      Elements are linked via pointers

    3. C

      Data is stored contiguously

    4. D

      Data is stored non-contiguously

    Correct answer
    C

    Data is stored contiguously

    Explanation

    Arrays store data in contiguous memory locations, which allows random access but makes resizing difficult.

  6. 6

    If an array of size 3 is full and you need to add a 4th element, what is a possible solution?

    1. A

      Allocate a larger array, copy old elements, and free the old array

    2. B

      Ignore the new element

    3. C

      Use pointer arithmetic to extend the array in place

    4. D

      Overwrite the third element

    Correct answer
    A

    Allocate a larger array, copy old elements, and free the old array

    Explanation

    When an array runs out of space, you must allocate a new larger array, copy the old elements, and free the old memory to maintain contiguity.

  7. 7

    What is the time complexity of copying an array of size n to a new array?

    1. A

      O(log⁡n)O(\log n)O(logn)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    Copying an array requires iterating over all n elements, so it is O(n)O(n)O(n).

  8. 8

    When using malloc to allocate memory, what value is returned if the allocation fails?

    1. A

      0

    2. B

      The address of a small default chunk

    3. C

      A random address

    4. D

      NULL

    Correct answer
    D

    NULL

    Explanation

    If malloc fails, it returns NULL (the null pointer), which should be checked to avoid undefined behavior.

  9. 9

    In C, what header file is needed to use malloc?

    1. A

      stdlib.h

    2. B

      stdio.h

    3. C

      string.h

    4. D

      math.h

    Correct answer
    A

    stdlib.h

    Explanation

    malloc is declared in stdlib.h.

  10. 10

    What is a node in a linked list?

    1. A

      A single cell containing data and a pointer to the next node

    2. B

      A pointer to the first element

    3. C

      An array of values

    4. D

      A special sentinel value

    Correct answer
    A

    A single cell containing data and a pointer to the next node

    Explanation

    A node in a linked list contains the data and a pointer (link) to the next node in the sequence.

  11. 11

    What is the time complexity of prepending an element to a linked list?

    1. A

      O(n)O(n)O(n)

    2. B

      O(log⁡n)O(\log n)O(logn)

    3. C

      O(1)O(1)O(1)

    4. D

      O(nlog⁡n)O(n \log n)O(nlogn)

    Correct answer
    C

    O(1)O(1)O(1)

    Explanation

    Prepending (adding at the beginning) only requires updating a couple of pointers, regardless of list size, so it is O(1)O(1)O(1).

  12. 12

    What is the time complexity of searching for a value in an unsorted linked list?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O(log⁡n)O(\log n)O(logn)

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    In the worst case, you must traverse the entire list, making search O(n)O(n)O(n).

  13. 13

    What is a major trade-off when using a linked list compared to an array?

    1. A

      Linked lists are always sorted but harder to implement

    2. B

      Linked lists can grow dynamically but require extra memory for pointers

    3. C

      Linked lists allow constant-time access to any element but use more memory

    4. D

      Linked lists use less memory but are slower to traverse

    Correct answer
    B

    Linked lists can grow dynamically but require extra memory for pointers

    Explanation

    Linked lists can grow and shrink dynamically without copying, but each node requires extra memory for the pointer, and random access is O(n)O(n)O(n).

  14. 14

    If a linked list is used to implement a queue, where should new elements be added to achieve FIFO behavior?

    1. A

      At the front (head) of the list

    2. B

      At the back (tail) of the list

    3. C

      In sorted order

    4. D

      At a random position

    Correct answer
    B

    At the back (tail) of the list

    Explanation

    For FIFO, elements are enqueued at the back and dequeued from the front. Adding at the end requires traversal unless you maintain a tail pointer.

  15. 15

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

    1. A

      For any node, all values in the left subtree are smaller, and all values in the right subtree are larger

    2. B

      The tree is always perfectly balanced

    3. C

      Every node has exactly two children

    4. D

      Nodes are stored in contiguous memory

    Correct answer
    A

    For any node, all values in the left subtree are smaller, and all values in the right subtree are larger

    Explanation

    A BST maintains sorted order: left subtree values are less than the node's value, and right subtree values are greater.

  16. 16

    In the worst-case scenario, what is the time complexity of searching a binary search tree?

    1. A

      O(1)O(1)O(1)

    2. B

      O(nlog⁡n)O(n \log n)O(nlogn)

    3. C

      O(n)O(n)O(n)

    4. D

      O(log⁡n)O(\log n)O(logn)

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    If the BST is unbalanced (e.g., inserting sorted data), it degenerates into a linked list, making search O(n)O(n)O(n).

  17. 17

    What recursive base case is used when searching a binary search tree?

    1. A

      If the value is less than the root, search left

    2. B

      If the tree is empty (NULL), return false

    3. C

      If the node has no children, return false

    4. D

      If the value is found, return true

    Correct answer
    B

    If the tree is empty (NULL), return false

    Explanation

    The base case for recursion in BST search is when the current node is NULL, meaning the value is not present, so return false.

  18. 18

    What is a hash table typically composed of?

    1. A

      A dynamic array

    2. B

      An array of linked lists

    3. C

      A single linked list

    4. D

      A binary tree

    Correct answer
    B

    An array of linked lists

    Explanation

    A hash table usually consists of an array, where each element is a pointer to a linked list (bucket) that handles collisions.

  19. 19

    What is a hash function?

    1. A

      A function that compresses data by removing characters

    2. B

      A function that stores data in sorted order

    3. C

      A function that duplicates data for redundancy

    4. D

      A function that maps arbitrary inputs to a fixed range of outputs

    Correct answer
    D

    A function that maps arbitrary inputs to a fixed range of outputs

    Explanation

    A hash function takes an input (e.g., a string) and produces an integer (hash value) within a fixed range, often used to index into an array.

  20. 20

    What is a collision in a hash table?

    1. A

      When the hash table runs out of memory

    2. B

      When the hash function fails

    3. C

      When two keys map to the same hash value

    4. D

      When a key is not found

    Correct answer
    C

    When two keys map to the same hash value

    Explanation

    A collision occurs when two different keys produce the same hash value, requiring a resolution strategy like chaining (linked lists).

  21. 21

    In a hash table with separate chaining, what is the worst-case time complexity for a search?

    1. A

      O(n)O(n)O(n)

    2. B

      O(log⁡n)O(\log n)O(logn)

    3. C

      O(n2)O(n^2)O(n2)

    4. D

      O(1)O(1)O(1)

    Correct answer
    A

    O(n)O(n)O(n)

    Explanation

    In the worst case, all keys hash to the same bucket, forming a single linked list of length n, making search O(n)O(n)O(n).

  22. 22

    What is the ideal time complexity for searching in a hash table with a perfect hash function?

    1. A

      O(1)O(1)O(1)

    2. B

      O(n)O(n)O(n)

    3. C

      O(log⁡n)O(\log n)O(logn)

    4. D

      O(nlog⁡n)O(n \log n)O(nlogn)

    Correct answer
    A

    O(1)O(1)O(1)

    Explanation

    A perfect hash function maps each key to a unique index, allowing constant-time access without collisions.

  23. 23

    What is a try (or trie) data structure?

    1. A

      An array of linked lists

    2. B

      A balanced binary search tree

    3. C

      A hash table with open addressing

    4. D

      A tree where each node is an array of pointers

    Correct answer
    D

    A tree where each node is an array of pointers

    Explanation

    A try (short for retrieval) is a tree where each node contains an array of pointers (e.g., 26 for letters), enabling fast prefix-based searches.

  24. 24

    What is the time complexity of searching for a key in a try, assuming the key length is bounded by a constant?

    1. A

      O(log⁡n)O(\log n)O(logn)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n)O(n)O(n)

    4. D

      O(k)O(k)O(k) where kkk is the number of keys

    Correct answer
    B

    O(1)O(1)O(1)

    Explanation

    Since the number of steps equals the length of the key (a constant), search time is O(1)O(1)O(1), independent of the number of keys stored.

  25. 25

    What is a major disadvantage of a try compared to a hash table?

    1. A

      It has slower average-case search time

    2. B

      It requires more memory due to many unused pointers

    3. C

      It does not support insertion

    4. D

      It cannot handle string keys

    Correct answer
    B

    It requires more memory due to many unused pointers

    Explanation

    Tries often waste memory because each node contains an array of pointers, many of which are NULL, especially for sparse data.

  26. 26

    Which data structure is an example of a dictionary (key-value store)?

    1. A

      A hash table

    2. B

      A queue

    3. C

      A linked list

    4. D

      A stack

    Correct answer
    A

    A hash table

    Explanation

    A dictionary associates keys with values; a hash table is a common implementation of this abstract data type.

  27. 27

    What is the main trade-off when choosing between arrays and linked lists?

    1. A

      Arrays are easier to code but linked lists are always faster

    2. B

      Arrays allow constant-time access and are memory-efficient but resizing is expensive; linked lists allow dynamic resizing but have slower access

    3. C

      Arrays use less memory but are slower to insert; linked lists use more memory but allow faster random access

    4. D

      Arrays are only for numbers; linked lists are for any data type

    Correct answer
    B

    Arrays allow constant-time access and are memory-efficient but resizing is expensive; linked lists allow dynamic resizing but have slower access

    Explanation

    Arrays provide O(1)O(1)O(1) random access and are compact, but resizing requires copying. Linked lists allow O(1)O(1)O(1) insertion/removal at ends but have O(n)O(n)O(n) access and extra pointer overhead.

  28. 28

    What does the term 'abstract data type' mean?

    1. A

      A data type that cannot be used in C

    2. B

      A data type that uses only primitive types

    3. C

      A data type that is implemented only in assembly language

    4. D

      A high-level description of a data structure with specified operations, independent of implementation

    Correct answer
    D

    A high-level description of a data structure with specified operations, independent of implementation

    Explanation

    An abstract data type (ADT) defines a set of operations and properties, leaving implementation details to the programmer (e.g., queue, stack).

Question 1 of 28
Question 1 of 28

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent