1. Home
  2. Explore
  3. Computer Science
  4. Stacks and Queues

Study methods

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

Stacks and Queues

  • 57Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Abstract Structures

What is the central trade-off described between C and higher-level languages such as Python?

57 questions

  1. 1

    What is the central trade-off described between C and higher-level languages such as Python?

    1. A

      C allows fewer programming techniques, but its programs always use more memory

    2. B

      Higher-level languages can make solving problems faster for humans, but their code may run less efficiently

    3. C

      Higher-level languages always produce faster machine code, but they take longer for humans to write

    4. D

      C and Python solve different classes of problems and therefore cannot be directly compared

    Correct answer
    B

    Higher-level languages can make solving problems faster for humans, but their code may run less efficiently

    Explanation

    Higher-level languages such as Python generally make programs quicker and easier for humans to write, while lower-level languages such as C may produce code that runs faster.

  2. 2

    What does it mean for a data structure to be an abstract data type?

    1. A

      Its implementation must use dynamically allocated memory rather than arrays

    2. B

      Its values are stored only in abstract mathematical objects rather than computer memory

    3. C

      Its supported operations and characteristics are defined independently of its implementation details

    4. D

      Its operations are limited to those provided directly by the programming language

    Correct answer
    C

    Its supported operations and characteristics are defined independently of its implementation details

    Explanation

    An abstract data type describes what a structure does and what features it offers, without requiring one particular low-level implementation.

  3. 3

    Which property characterizes a queue?

    1. A

      Randomly inserted, randomly removed

    2. B

      Smallest value, first out

    3. C

      Last in, first out

    4. D

      First in, first out

    Correct answer
    D

    First in, first out

    Explanation

    A queue follows the FIFO property: the first item inserted is the first item removed.

  4. 4

    Which operations are fundamental to a queue?

    1. A

      Push at the front and pop from the back

    2. B

      Insert at the front and remove from the middle

    3. C

      Search from the middle and delete from either end

    4. D

      Enqueue at the back and dequeue from the front

    Correct answer
    D

    Enqueue at the back and dequeue from the front

    Explanation

    Enqueue means adding an item to the queue, while dequeue means removing an item from the front.

  5. 5

    Which property characterizes a stack?

    1. A

      Smallest value, first out

    2. B

      First in, first out

    3. C

      Last in, first out

    4. D

      Oldest value, first out

    Correct answer
    C

    Last in, first out

    Explanation

    A stack follows the LIFO property: the most recently inserted item is removed first.

  6. 6

    Which operations are fundamental to a stack?

    1. A

      Hash and search

    2. B

      Append and traverse

    3. C

      Enqueue and dequeue

    4. D

      Push and pop

    Correct answer
    D

    Push and pop

    Explanation

    Push adds an item to the top of a stack, and pop removes an item from the top.

  7. 7

    Why might a stack be preferable to a queue for distributing cafeteria trays?

    1. A

      The oldest tray is always guaranteed to be the cleanest

    2. B

      A stack uses no memory to keep track of its trays

    3. C

      A stack automatically sorts trays by their size

    4. D

      The most recently placed tray is easiest to access from the top

    Correct answer
    D

    The most recently placed tray is easiest to access from the top

    Explanation

    Because trays are placed on top of one another, removing the most recently placed top tray is convenient, illustrating LIFO behavior.

  8. 8

    Why can a queue or stack implemented with a fixed-size array be problematic?

    1. A

      It always removes values in the wrong order

    2. B

      It cannot be accessed using indexes or pointers

    3. C

      It cannot store values of the same data type in adjacent locations

    4. D

      It has a predetermined capacity that may be too small or may waste memory

    Correct answer
    D

    It has a predetermined capacity that may be too small or may waste memory

    Explanation

    A fixed-size array requires choosing its capacity in advance. A capacity that is too small cannot hold all items, while an oversized capacity wastes memory.

  9. 9

    What distinguishes an array's capacity from its current size?

    1. A

      Capacity describes the element type, while size describes the memory address

    2. B

      Capacity is the number currently stored, while size is the maximum possible number

    3. C

      Capacity is the maximum number of elements, while size is the number currently stored

    4. D

      Capacity and size are interchangeable terms for the same quantity

    Correct answer
    C

    Capacity is the maximum number of elements, while size is the number currently stored

    Explanation

    Capacity refers to the total space available, whereas size refers to how many elements are actually present at a given time.

  10. 10

    What is the defining memory-layout property of an array?

    1. A

      Its values are stored in sorted order regardless of insertion order

    2. B

      Its values are stored in separate locations connected by pointers

    3. C

      Its values are stored contiguously, or back to back, in memory

    4. D

      Its values are stored only in dynamically allocated memory blocks

    Correct answer
    C

    Its values are stored contiguously, or back to back, in memory

    Explanation

    An array is a contiguous chunk of memory in which elements occupy consecutive locations.

  11. 11

    Why can an existing fixed-size array not simply receive a fourth element immediately after its third element?

    1. A

      The operating system reserves all memory after an array permanently

    2. B

      Arrays require every element to have a different data type

    3. C

      The memory immediately after the array may already belong to another object

    4. D

      Array indexes cannot represent values greater than three

    Correct answer
    C

    The memory immediately after the array may already belong to another object

    Explanation

    Declaring an array reserves only its specified block. The adjacent memory may contain another object or otherwise not be available for the array.

  12. 12

    What must generally happen when enlarging an array using a newly allocated block of memory?

    1. A

      The original block must remain allocated permanently to preserve the array

    2. B

      The old elements can be ignored because their values remain automatically accessible

    3. C

      The new block must be filled with zeros before the old block can be accessed

    4. D

      The old elements must be copied, the new element added, and the old block released

    Correct answer
    D

    The old elements must be copied, the new element added, and the old block released

    Explanation

    A larger replacement block must receive copies of the old elements, after which the original memory should be freed.

  13. 13

    What is a memory leak in the array-resizing example?

    1. A

      Freeing memory while another pointer still refers to it

    2. B

      Using array notation to access dynamically allocated memory

    3. C

      Allocating memory with a size larger than one byte

    4. D

      Losing the pointer to allocated memory before freeing that memory

    Correct answer
    D

    Losing the pointer to allocated memory before freeing that memory

    Explanation

    Assigning a new allocation directly to the only pointer can make the original allocation unreachable, preventing the program from freeing it.

  14. 14

    Why is a temporary pointer useful when replacing an allocation with a larger one?

    1. A

      It automatically converts an array into a linked list

    2. B

      It allows the program to avoid freeing either allocation

    3. C

      It prevents the operating system from tracking the allocated memory

    4. D

      It preserves access to the new allocation until copying and error checks are complete

    Correct answer
    D

    It preserves access to the new allocation until copying and error checks are complete

    Explanation

    A temporary pointer lets the program retain the original pointer while it checks whether the new allocation succeeded and copies the old values.

  15. 15

    What should a program do after a memory-allocation function returns NULL?

    1. A

      Assume the operating system will automatically initialize the pointer

    2. B

      Dereference the pointer because NULL represents valid empty memory

    3. C

      Treat the allocation as unsuccessful and handle the error safely

    4. D

      Call the allocation function repeatedly without checking its result

    Correct answer
    C

    Treat the allocation as unsuccessful and handle the error safely

    Explanation

    A NULL result indicates that the requested allocation failed. The program should not dereference it and should handle the failure appropriately.

  16. 16

    Why should dynamically allocated memory generally be freed even though the operating system reclaims it when a program exits?

    1. A

      Long-running programs can otherwise accumulate leaks and waste memory

    2. B

      Freeing memory makes all future allocations occur in contiguous locations

    3. C

      The operating system cannot reclaim memory from a terminated program

    4. D

      Freeing memory is required for every pointer to remain non-NULL

    Correct answer
    A

    Long-running programs can otherwise accumulate leaks and waste memory

    Explanation

    Although termination usually returns memory to the operating system, programs that run for a long time can suffer serious memory loss if allocations are not freed.

  17. 17

    What advantage does realloc provide when enlarging an existing allocation?

    1. A

      It automatically determines which new values the programmer wants to add

    2. B

      It eliminates the need to check whether the operation returned NULL

    3. C

      It guarantees that the block will always remain at its original address

    4. D

      It may extend the existing block or move and copy the data automatically

    Correct answer
    D

    It may extend the existing block or move and copy the data automatically

    Explanation

    realloc attempts to grow the existing block in place. If that is impossible, it can allocate another block and copy the existing data.

  18. 18

    Why should the result of realloc usually be stored in a temporary pointer first?

    1. A

      A failure can return NULL while leaving the original allocation intact

    2. B

      The original pointer cannot be used to access the enlarged block

    3. C

      A direct assignment prevents realloc from copying old data

    4. D

      The temporary pointer is required to convert bytes into integers

    Correct answer
    A

    A failure can return NULL while leaving the original allocation intact

    Explanation

    If realloc fails, it returns NULL without changing the original block. Overwriting the original pointer immediately would lose access to that block.

  19. 19

    Why is a linked list more dynamically resizable than a fixed-size array?

    1. A

      A linked list stores no metadata alongside its values

    2. B

      All linked-list elements are guaranteed to occupy consecutive memory locations

    3. C

      A linked list automatically doubles its capacity after every insertion

    4. D

      Each new element can be allocated separately and linked without moving existing elements

    Correct answer
    D

    Each new element can be allocated separately and linked without moving existing elements

    Explanation

    Linked-list nodes can be placed wherever sufficient memory is available, and pointers can connect them without copying the existing nodes.

  20. 20

    What metadata does a typical singly linked-list node contain in addition to its data?

    1. A

      A pointer to the next node

    2. B

      A pointer to every node in the list

    3. C

      The total capacity of the computer's memory

    4. D

      A copy of the entire list's contents

    Correct answer
    A

    A pointer to the next node

    Explanation

    A singly linked-list node normally stores its data and one pointer containing the address of the next node.

  21. 21

    How does a linked list represent the end of the list?

    1. A

      The final node's next pointer contains NULL

    2. B

      The final node's data field contains zero

    3. C

      The final node is placed immediately after the first node in memory

    4. D

      The first node's address is replaced with the final node's address

    Correct answer
    A

    The final node's next pointer contains NULL

    Explanation

    A NULL next pointer indicates that no node follows the current node.

  22. 22

    What does the list pointer represent in a singly linked list?

    1. A

      The address of the first node

    2. B

      The address of the next unused memory location

    3. C

      The address of the final node

    4. D

      The number of nodes currently in the list

    Correct answer
    A

    The address of the first node

    Explanation

    The list itself can be represented by a pointer to its first node. From there, the remaining nodes can be reached by following next pointers.

  23. 23

    What is the purpose of the C arrow operator, such as node->next?

    1. A

      It allocates a new structure immediately after the current structure

    2. B

      It converts a structure into an array of pointers

    3. C

      It compares two pointers to determine which address is larger

    4. D

      It dereferences a pointer and accesses a field within the pointed-to structure

    Correct answer
    D

    It dereferences a pointer and accesses a field within the pointed-to structure

    Explanation

    The arrow operator combines dereferencing a structure pointer with accessing one of the structure's fields.

  24. 24

    Why does a self-referential node declaration use struct node *next inside the structure?

    1. A

      The field prevents the compiler from reading the structure definition

    2. B

      The field must point to another object of the same structure type

    3. C

      The field must point only to an integer rather than another node

    4. D

      The field stores the complete next structure directly inside the current one

    Correct answer
    B

    The field must point to another object of the same structure type

    Explanation

    A linked-list node needs a pointer to another node. A pointer can refer to the structure type without embedding an infinitely large structure inside itself.

  25. 25

    What is the running time of prepending a node to a linked list when the list's first-node pointer is already available?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Prepending requires only a constant number of pointer assignments, regardless of the list length, so it is O(1)O(1)O(1).

  26. 26

    What is the worst-case running time of searching an unspecialized linked list?

    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

    In the worst case, the target is at the end of the list or absent, requiring traversal of all nnn nodes.

  27. 27

    Why does appending to a linked list take O(n)O(n)O(n) time when no tail pointer is maintained?

    1. A

      The program must compare the new value with every possible integer

    2. B

      The program must sort all values before adding the new node

    3. C

      The program must traverse the list to find the final node

    4. D

      The program must copy every existing node into a larger contiguous block

    Correct answer
    C

    The program must traverse the list to find the final node

    Explanation

    Without a pointer to the last node, the program must follow next pointers from the beginning until it finds a node whose next field is NULL.

  28. 28

    What is the main advantage of a doubly linked list over a singly linked list?

    1. A

      All nodes become contiguous in memory

    2. B

      Each node can point both to the next node and to the previous node

    3. C

      Searching becomes automatically constant time

    4. D

      Each node requires no additional metadata

    Correct answer
    B

    Each node can point both to the next node and to the previous node

    Explanation

    A doubly linked list adds a pointer in the reverse direction. This can simplify traversal and some insertion or deletion operations, although it does not fundamentally change the asymptotic search speed.

  29. 29

    Why does a linked list generally not support binary search as efficiently as an array?

    1. A

      Linked lists contain too many values for comparisons

    2. B

      Binary search requires every value to have two pointers

    3. C

      Finding the middle requires traversing nodes because their addresses are not directly calculable

    4. D

      Linked lists cannot store sorted values

    Correct answer
    C

    Finding the middle requires traversing nodes because their addresses are not directly calculable

    Explanation

    Array elements are contiguous, so their addresses can be calculated directly. Linked-list nodes may be scattered, so reaching the middle requires traversal.

  30. 30

    What is a tree's root?

    1. A

      The final node reached during a traversal

    2. B

      The topmost node from which the tree's other nodes descend

    3. C

      Any node that has exactly two children

    4. D

      The node with the smallest stored value

    Correct answer
    B

    The topmost node from which the tree's other nodes descend

    Explanation

    The root is the top node of a tree and serves as the starting point for reaching its descendants.

  31. 31

    What makes a binary search tree different from an arbitrary binary tree?

    1. A

      All nodes must be stored contiguously in memory

    2. B

      Every node must have exactly two children

    3. C

      Each node's left subtree contains smaller values and its right subtree contains larger values

    4. D

      The root must contain the largest value in the tree

    Correct answer
    C

    Each node's left subtree contains smaller values and its right subtree contains larger values

    Explanation

    A binary search tree maintains an ordering property: values in the left subtree are smaller and values in the right subtree are larger, assuming distinct values.

  32. 32

    Why is the binary search tree ordering property recursive?

    1. A

      Each node must call the operating system before receiving children

    2. B

      The tree can only be searched using a recursive programming language

    3. C

      Every subtree must contain exactly the same number of nodes

    4. D

      The same left-smaller and right-larger rule applies to each node's subtrees

    Correct answer
    D

    The same left-smaller and right-larger rule applies to each node's subtrees

    Explanation

    A binary search tree is defined so that the ordering rule holds not only at the root but also at every subtree.

  33. 33

    What is the typical search time in a balanced binary search tree?

    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
    B

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

    Explanation

    At each level of a balanced binary search tree, the search can discard roughly half the remaining possibilities, producing O(log⁡n)O(\log n)O(logn) search time.

  34. 34

    What is a leaf node in a tree?

    1. A

      A node with no children

    2. B

      The root node with the greatest value

    3. C

      The first node inserted into the tree

    4. D

      A node that must have exactly one child

    Correct answer
    A

    A node with no children

    Explanation

    Leaf nodes occur at the edge of the tree and have no child nodes.

  35. 35

    How can a binary search tree lose its logarithmic performance?

    1. A

      Using pointers causes every comparison to require a full memory scan

    2. B

      Storing values in sorted order always makes the tree slower than an array

    3. C

      Adding a root node automatically removes all child nodes

    4. D

      Inserting values in an unfortunate order can make the tree highly unbalanced

    Correct answer
    D

    Inserting values in an unfortunate order can make the tree highly unbalanced

    Explanation

    For example, inserting increasingly larger values can create a tree resembling a linked list, causing operations to degrade to O(n)O(n)O(n).

  36. 36

    What is the purpose of rebalancing a binary search tree?

    1. A

      To preserve a small tree height and maintain approximately logarithmic operations

    2. B

      To convert the tree into a fixed-size array

    3. C

      To eliminate the need for pointers between nodes

    4. D

      To make every node contain the same numerical value

    Correct answer
    A

    To preserve a small tree height and maintain approximately logarithmic operations

    Explanation

    Rebalancing reorganizes nodes so that the tree does not become a long chain, helping preserve O(log⁡n)O(\log n)O(logn) performance.

  37. 37

    Which condition is the base case in the recursive binary-search-tree function described?

    1. A

      The current node contains a value smaller than the target

    2. B

      The tree pointer is NULL, so the requested value is absent

    3. C

      The current node is located at the bottom of the computer's memory

    4. D

      The current node has two children, so the search must stop

    Correct answer
    B

    The tree pointer is NULL, so the requested value is absent

    Explanation

    A NULL subtree contains no nodes, so the search can immediately return false.

  38. 38

    When searching a binary search tree, where should the recursive call go if the target is smaller than the current node's value?

    1. A

      To the current node's right subtree

    2. B

      To the root's parent subtree

    3. C

      To every leaf in the tree

    4. D

      To the current node's left subtree

    Correct answer
    D

    To the current node's left subtree

    Explanation

    The ordering property guarantees that smaller values are found in the left subtree.

  39. 39

    What is a dictionary in the abstract data-type sense?

    1. A

      A collection of key-value pairs

    2. B

      A tree whose nodes can contain only definitions

    3. C

      A collection of values stored only in numerical order

    4. D

      A queue in which every item has the same priority

    Correct answer
    A

    A collection of key-value pairs

    Explanation

    A dictionary associates each key, such as a word or name, with a corresponding value, such as a definition or phone number.

  40. 40

    What does a hash function do?

    1. A

      It sorts every value by repeatedly comparing adjacent elements

    2. B

      It converts every key into a unique value without possible collisions

    3. C

      It stores the entire data structure in one contiguous memory block

    4. D

      It maps an input key from a large or infinite domain to a finite bucket index

    Correct answer
    D

    It maps an input key from a large or infinite domain to a finite bucket index

    Explanation

    A hash function takes an input such as a name and produces a finite-range output identifying a bucket.

  41. 41

    If a hash table uses one bucket for each first letter of an English name, what index should a name beginning with M receive when indexing starts at zero?

    1. A

      25

    2. B

      11

    3. C

      13

    4. D

      12

    Correct answer
    D

    12

    Explanation

    M is the thirteenth letter of the alphabet, but with zero-based indexing its bucket index is 121212.

  42. 42

    Why are collisions unavoidable when mapping an infinite domain to a finite range?

    1. A

      Multiple distinct inputs must sometimes map to the same output bucket

    2. B

      A collision occurs only when two keys are identical

    3. C

      Every hash function necessarily returns a negative value

    4. D

      Finite ranges cannot store any input values at all

    Correct answer
    A

    Multiple distinct inputs must sometimes map to the same output bucket

    Explanation

    When more possible inputs exist than output buckets, different inputs must share some buckets. This is a collision.

  43. 43

    How does a hash table commonly handle multiple keys that hash to the same bucket?

    1. A

      It deletes the earlier entry whenever a collision occurs

    2. B

      It places all colliding entries in one array position without metadata

    3. C

      It changes every existing key to match the newest key

    4. D

      It stores the colliding entries in a linked list associated with that bucket

    Correct answer
    D

    It stores the colliding entries in a linked list associated with that bucket

    Explanation

    A hash table can use an array of buckets, with each bucket pointing to a linked list containing all entries that hash there.

  44. 44

    What is the main trade-off of using a hash function based on more characters of a name?

    1. A

      It increases collisions while guaranteeing less memory usage

    2. B

      It prevents names from being stored in linked lists

    3. C

      It can reduce collisions but requires many more buckets and more memory

    4. D

      It makes the hash function independent of the input key

    Correct answer
    C

    It can reduce collisions but requires many more buckets and more memory

    Explanation

    Hashing on more characters creates a larger range of possible buckets, which can reduce collisions but may waste substantial memory.

  45. 45

    Under a uniform distribution across a fixed number of buckets, why can hash-table search still be described as O(n)O(n)O(n) asymptotically?

    1. A

      The table stores all entries in one linked list regardless of their hashes

    2. B

      The hash function cannot calculate a bucket index in constant time

    3. C

      Each bucket's linked list may contain approximately n/kn/kn/k entries, and kkk is treated as a constant

    4. D

      Hashing always requires examining every bucket before searching any list

    Correct answer
    C

    Each bucket's linked list may contain approximately n/kn/kn/k entries, and kkk is treated as a constant

    Explanation

    With kkk fixed buckets, an average chain may have length n/kn/kn/k. Although this can be much faster in practice, dividing by a constant does not change the asymptotic classification from O(n)O(n)O(n).

  46. 46

    Why might a hash table be described as an array of linked lists?

    1. A

      The structure uses arrays only for values and linked lists only for indexes

    2. B

      The array selects a bucket, and the linked list stores entries that collide in that bucket

    3. C

      The linked list selects a bucket, and the array stores every entry contiguously

    4. D

      Each array element contains a complete copy of every linked list

    Correct answer
    B

    The array selects a bucket, and the linked list stores entries that collide in that bucket

    Explanation

    Each array slot is typically a pointer to a linked list. The hash function selects the slot, and the list handles collisions.

  47. 47

    What does const communicate when used with a string parameter passed by reference?

    1. A

      The called function should not modify the referenced string through that parameter

    2. B

      The pointer is guaranteed to contain a non-NULL address

    3. C

      The string must contain only uppercase letters

    4. D

      The function receives a complete copy of the string rather than an address

    Correct answer
    A

    The called function should not modify the referenced string through that parameter

    Explanation

    const protects the referenced value from modification through that parameter, even though the function receives an address rather than a copy.

  48. 48

    Why might a hash function return an unsigned integer?

    1. A

      Unsigned integers require no memory allocation

    2. B

      An unsigned integer automatically prevents collisions

    3. C

      A hash function can return only unsigned values in C

    4. D

      A bucket index should not be negative

    Correct answer
    D

    A bucket index should not be negative

    Explanation

    Array indexes and bucket numbers should be nonnegative, so an unsigned integer communicates that intended restriction.

  49. 49

    What is a trie?

    1. A

      A queue that removes words according to alphabetical order

    2. B

      A fixed-size array that stores all strings contiguously

    3. C

      A tree whose nodes contain arrays of pointers representing possible next characters

    4. D

      A linked list in which every node stores three numerical values

    Correct answer
    C

    A tree whose nodes contain arrays of pointers representing possible next characters

    Explanation

    A trie is a tree of arrays. Each node contains pointers for possible next characters, allowing strings to be represented by paths.

  50. 50

    How is a word such as Toad represented implicitly in a trie?

    1. A

      By storing only the final character and discarding the preceding characters

    2. B

      By following non-NULL pointers for T, O, A, and D and marking the word's endpoint

    3. C

      By placing the word in the first available linked-list bucket

    4. D

      By storing the complete string in one contiguous array at the root

    Correct answer
    B

    By following non-NULL pointers for T, O, A, and D and marking the word's endpoint

    Explanation

    The path through character-indexed pointers represents the letters of the word, and a Boolean or equivalent marker identifies where the word ends.

  51. 51

    Why does a trie need an endpoint marker for each stored word?

    1. A

      The marker records how many times the word was searched

    2. B

      The marker prevents all words from sharing any nodes

    3. C

      The marker stores the memory address of the root node

    4. D

      The marker distinguishes a complete word from a prefix of a longer word

    Correct answer
    D

    The marker distinguishes a complete word from a prefix of a longer word

    Explanation

    Words can be prefixes of other words, so a Boolean endpoint marker indicates that a valid word ends at that node.

  52. 52

    How does a trie search for a word?

    1. A

      It follows the pointer corresponding to each successive character and checks the endpoint marker

    2. B

      It follows only the pointers corresponding to the word's final character

    3. C

      It scans every stored word from the beginning until it finds an exact match

    4. D

      It repeatedly divides a contiguous array in half based on numerical values

    Correct answer
    A

    It follows the pointer corresponding to each successive character and checks the endpoint marker

    Explanation

    The search uses each character to choose the next pointer. If any required pointer is absent, the word is absent; otherwise, the endpoint marker confirms whether the complete word exists.

  53. 53

    Why can trie lookup be considered constant time for a bounded vocabulary of names?

    1. A

      The trie stores all names in one memory location

    2. B

      The number of stored names is always fixed at 26

    3. C

      The trie never needs to inspect characters in the searched name

    4. D

      The number of pointer steps depends on maximum name length rather than the number of stored names

    Correct answer
    D

    The number of pointer steps depends on maximum name length rather than the number of stored names

    Explanation

    For a fixed maximum key length, lookup takes a bounded number of character steps, independent of whether the trie contains a few names or millions.

  54. 54

    What is the principal disadvantage of a trie?

    1. A

      It can consume substantial memory because many child pointers are unused

    2. B

      It cannot associate a key with a value such as a phone number

    3. C

      It requires all words to have the same length

    4. D

      It cannot represent words that share prefixes

    Correct answer
    A

    It can consume substantial memory because many child pointers are unused

    Explanation

    Each trie node may contain an array of many child pointers, most of which can be NULL, leading to considerable wasted space.

  55. 55

    Which data structure is especially suitable for associating names with phone numbers?

    1. A

      A queue containing names in insertion order

    2. B

      A stack containing only phone numbers

    3. C

      A dictionary implemented with a hash table

    4. D

      A fixed array that stores only the names

    Correct answer
    C

    A dictionary implemented with a hash table

    Explanation

    A dictionary or hash table is designed to associate keys, such as names, with values, such as phone numbers.

  56. 56

    Why can a restaurant shelf system for pickup orders be viewed as a hash table?

    1. A

      Orders are removed strictly in the order they were prepared

    2. B

      Every order is placed randomly and later found by scanning every shelf

    3. C

      Each shelf stores only one order and cannot contain collisions

    4. D

      A name is mapped to a bucket or shelf range where the corresponding order is placed

    Correct answer
    D

    A name is mapped to a bucket or shelf range where the corresponding order is placed

    Explanation

    The customer's name is hashed into a range of shelves, much like a key is mapped to a bucket in a hash table.

  57. 57

    What does the final C program discussed need to do for the spell-checking task?

    1. A

      Store every word in a fixed array whose size cannot change

    2. B

      Use a stack so that the newest dictionary word is checked first

    3. C

      Sort only the misspelled words after the program finishes

    4. D

      Load a large dictionary efficiently and allow the program's speed and memory use to be evaluated

    Correct answer
    D

    Load a large dictionary efficiently and allow the program's speed and memory use to be evaluated

    Explanation

    The spell-checking task involves loading more than 100,000 English words and comparing implementation choices based on running time and memory usage.

Question 1 of 57
Question 1 of 57

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent