1. Home
  2. Explore
  3. Computer Science
  4. Data Structures Course

Study methods

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

Data Structures Course

  • 87Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Arrays

What is the time complexity of accessing an element by index in a static array?

87 questions

  1. 1

    What is the time complexity of accessing an element by index in a static array?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    C

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

    Explanation

    Array elements are stored in contiguous memory locations, so the address of index iii is computed as base address +i×+ i \times+i× element size, allowing direct access in constant time.

  2. 2

    Why does accessing an element by index in an array take O(1)O(1)O(1) time?

    1. A

      Because the array is stored in contiguous memory and the address can be computed arithmetically

    2. B

      Because the array is stored in a linked list structure

    3. C

      Because the array always has a fixed size

    4. D

      Because the array uses a hash function to locate elements

    Correct answer
    A

    Because the array is stored in contiguous memory and the address can be computed arithmetically

    Explanation

    Contiguous memory allocation allows direct calculation of the memory address of any index using the base address and offset, avoiding traversal.

  3. 3

    What is the time complexity of inserting an element at the beginning of a fixed-size array?

    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
    B

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

    Explanation

    Inserting at the beginning requires shifting all existing elements one position to the right to make space, which takes O(n)O(n)O(n) time where nnn is the number of elements.

  4. 4

    In a fixed-size array, what happens to the last element when you insert an element in the middle?

    1. A

      It is lost because there is no space

    2. B

      It is placed at the beginning of the array

    3. C

      It remains unchanged

    4. D

      It is moved to the next index

    Correct answer
    A

    It is lost because there is no space

    Explanation

    Since the array has a fixed size, inserting in the middle requires shifting elements to the right, causing the last element to be overwritten and lost.

  5. 5

    What typically happens when a dynamic array runs out of capacity?

    1. A

      The array automatically compresses its elements

    2. B

      The program crashes

    3. C

      A new larger array is allocated, elements are copied, and the old array is discarded

    4. D

      Elements are stored in a linked list instead

    Correct answer
    C

    A new larger array is allocated, elements are copied, and the old array is discarded

    Explanation

    Dynamic arrays often double their size when full, allocate new memory, copy existing elements, and free the old array to accommodate more elements.

  6. 6

    What is the amortized time complexity of appending an element to a dynamic array?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Although occasional resizing costs O(n)O(n)O(n), the cost is spread over many insertions, resulting in O(1)O(1)O(1) amortized time per append.

  7. 7

    What is the primary advantage of a dynamic array over a static array?

    1. A

      It uses less memory

    2. B

      It can grow and shrink in size as needed

    3. C

      It does not require contiguous memory

    4. D

      It has faster access time

    Correct answer
    B

    It can grow and shrink in size as needed

    Explanation

    Dynamic arrays can resize automatically, allowing them to accommodate varying numbers of elements without predefined capacity.

  8. 8

    What is the time complexity of initializing a fixed-size array of size nnn?

    1. A

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

    2. B

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

    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

    Initializing a fixed-size array requires allocating and setting each of the nnn memory slots, which takes O(n)O(n)O(n) time.

  9. 9

    In an array, how is the memory address of an element at index iii computed?

    1. A

      base address ×i\times i×i

    2. B

      base address +i×+ i \times+i× size of element

    3. C

      base address +(i−1)×+ (i-1) \times+(i−1)× size of element

    4. D

      base address +i+ i+i

    Correct answer
    B

    base address +i×+ i \times+i× size of element

    Explanation

    Contiguous memory layout allows the address of index iii to be calculated as the base address plus the offset iii times the element size.

  10. 10

    Which of the following operations on a static array takes O(1)O(1)O(1) time?

    1. A

      Inserting an element at the beginning

    2. B

      Deleting an element from the middle

    3. C

      Accessing an element by index

    4. D

      Inserting an element in the middle

    Correct answer
    C

    Accessing an element by index

    Explanation

    Accessing by index uses direct address calculation and is independent of array size, making it O(1)O(1)O(1).

  11. 11

    What does each node in a singly linked list contain?

    1. A

      A value, a next pointer, and a previous pointer

    2. B

      A value and a pointer to the next node

    3. C

      A value and a pointer to the previous node

    4. D

      A value and an index

    Correct answer
    B

    A value and a pointer to the next node

    Explanation

    A singly linked list node consists of a value (data) and a 'next' pointer that references the subsequent node in the list.

  12. 12

    In a singly linked list, how do you access the last element?

    1. A

      By accessing the node stored at a special memory address

    2. B

      By traversing from the head until the next pointer is null

    3. C

      By directly using the tail pointer

    4. D

      By using the index of the last node

    Correct answer
    B

    By traversing from the head until the next pointer is null

    Explanation

    Since there is no direct tail reference in a basic singly linked list, you must traverse from the head node, following next pointers until reaching a node whose next is null.

  13. 13

    What is the time complexity of inserting a node at the beginning of a singly linked list (with a head pointer)?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    D

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

    Explanation

    Inserting at the beginning involves creating a new node, setting its next to the current head, and updating the head pointer—all constant-time operations.

  14. 14

    In a singly linked list with only a head pointer, why does inserting at the end take O(n)O(n)O(n) time?

    1. A

      Because the head pointer must be updated

    2. B

      Because you must traverse the entire list to find the last node

    3. C

      Because you need to shift all elements

    4. D

      Because you need to resize the list

    Correct answer
    B

    Because you must traverse the entire list to find the last node

    Explanation

    Without a tail pointer, you must start at the head and follow next pointers until you reach the last node, which requires O(n)O(n)O(n) steps.

  15. 15

    How does a circular singly linked list improve insertion/deletion at the end?

    1. A

      By using an array for storage

    2. B

      By storing a tail pointer that points to the last node

    3. C

      By increasing the size of each node

    4. D

      By using a doubly linked list structure

    Correct answer
    B

    By storing a tail pointer that points to the last node

    Explanation

    In a circular singly linked list, we can maintain a tail pointer. Since the tail's next points to the head, inserting at the end becomes O(1)O(1)O(1) by updating the tail's next.

  16. 16

    In a circular singly linked list, what does the next pointer of the tail node point to?

    1. A

      null

    2. B

      The head node

    3. C

      Itself

    4. D

      The previous node

    Correct answer
    B

    The head node

    Explanation

    To make the list circular, the last node's next pointer references the head node, forming a cycle.

  17. 17

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

    1. A

      It allows traversal in both directions and constant-time deletion when node is known

    2. B

      It uses less memory

    3. C

      It does not require pointers

    4. D

      It has faster access by index

    Correct answer
    A

    It allows traversal in both directions and constant-time deletion when node is known

    Explanation

    Doubly linked lists have a prev pointer, enabling backward traversal and O(1)O(1)O(1) deletion of a given node because the previous node is directly accessible.

  18. 18

    If you have a pointer to a node in a doubly linked list, what is the time complexity of deleting that node?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    With access to the node, you can update its previous node's next and its next node's prev in constant time, then free the node.

  19. 19

    What is the disadvantage of a doubly linked list compared to a singly linked list?

    1. A

      It uses more memory per node due to the extra prev pointer

    2. B

      It does not support deletion

    3. C

      It is slower for traversal

    4. D

      It cannot be made circular

    Correct answer
    A

    It uses more memory per node due to the extra prev pointer

    Explanation

    Each node in a doubly linked list stores an additional prev pointer, increasing memory usage compared to a singly linked list.

  20. 20

    How would you implement a queue using a singly linked list to achieve O(1)O(1)O(1) enqueue and dequeue?

    1. A

      Store only a head pointer and enqueue at head, dequeue at tail

    2. B

      Store both head and tail pointers; enqueue at tail, dequeue at head

    3. C

      Use an array instead of a linked list

    4. D

      Store only a tail pointer and use circular references

    Correct answer
    B

    Store both head and tail pointers; enqueue at tail, dequeue at head

    Explanation

    With head and tail pointers, enqueue (add at tail) can be done in O(1)O(1)O(1) by updating tail's next, and dequeue (remove from head) in O(1)O(1)O(1) by updating head.

  21. 21

    What does FIFO stand for in the context of queues?

    1. A

      First In, First Out

    2. B

      Fast Input, Fast Output

    3. C

      File Input/Output

    4. D

      First In, Last Out

    Correct answer
    A

    First In, First Out

    Explanation

    FIFO is the fundamental property of a queue: the first element added is the first one to be removed.

  22. 22

    What is the time complexity of enqueue operation in a queue implemented with a singly linked list with head and tail pointers?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Enqueue adds an element at the tail; with a tail pointer, we can directly link the new node after the tail in constant time.

  23. 23

    Why is using an array to implement a queue problematic for dequeue?

    1. A

      Because dequeue from the front requires shifting all remaining elements, which takes O(n)O(n)O(n) time

    2. B

      Because arrays have fixed size

    3. C

      Because arrays are not linear data structures

    4. D

      Because arrays do not support removing elements

    Correct answer
    A

    Because dequeue from the front requires shifting all remaining elements, which takes O(n)O(n)O(n) time

    Explanation

    When the front of the array is used as the queue front, removing an element causes a gap, and shifting all subsequent elements to fill it takes O(n)O(n)O(n) time.

  24. 24

    In a stack, what is the order of element removal?

    1. A

      First In, First Out

    2. B

      Last In, First Out

    3. C

      Sorted order

    4. D

      Random order

    Correct answer
    B

    Last In, First Out

    Explanation

    A stack follows LIFO (Last In, First Out): the most recently added element is the first to be removed.

  25. 25

    What data structure can be used to implement a stack with O(1)O(1)O(1) push and pop?

    1. A

      Singly linked list (operating at the head)

    2. B

      Queue

    3. C

      Array (operating at the end)

    4. D

      Both array (end) and singly linked list (head)

    Correct answer
    D

    Both array (end) and singly linked list (head)

    Explanation

    If we push and pop at the end of an array, both operations are O(1)O(1)O(1). Similarly, pushing and popping at the head of a singly linked list is O(1)O(1)O(1).

  26. 26

    How can two stacks be used to implement a queue?

    1. A

      One stack for input and one for output; during enqueue, transfer all elements to output, push new element, then transfer back to input

    2. B

      It is not possible to implement a queue with two stacks

    3. C

      By using both stacks simultaneously for each operation

    4. D

      One stack for even elements and one for odd elements

    Correct answer
    A

    One stack for input and one for output; during enqueue, transfer all elements to output, push new element, then transfer back to input

    Explanation

    The common method uses an input stack and an output stack. For enqueue, elements are moved to the output stack (reversing order), the new element is pushed onto the input stack, then elements are moved back, maintaining FIFO order.

  27. 27

    In the two-stack queue implementation, which operation typically becomes O(n)O(n)O(n)?

    1. A

      peek

    2. B

      isEmpty

    3. C

      dequeue

    4. D

      enqueue

    Correct answer
    D

    enqueue

    Explanation

    Enqueue requires moving all elements from the input stack to the output stack and back, which takes O(n)O(n)O(n) time in the worst case.

  28. 28

    What is a real-world example of a stack?

    1. A

      A playlist of songs played in order

    2. B

      A list of tasks to be done in any order

    3. C

      A pile of plates where you take from the top

    4. D

      A line of people waiting for service

    Correct answer
    C

    A pile of plates where you take from the top

    Explanation

    A stack of plates: the last plate placed on top is the first one removed, which matches the LIFO property of a stack.

  29. 29

    What operation in a queue returns the front element without removing it?

    1. A

      push

    2. B

      peek

    3. C

      size

    4. D

      pop

    Correct answer
    B

    peek

    Explanation

    Peek (also called front) returns the element at the front of the queue without dequeuing it, allowing inspection without modification.

  30. 30

    What property must be maintained for both push and pop operations in a stack?

    1. A

      They must be recursive

    2. B

      They must be performed on opposite ends

    3. C

      They must be performed on the same end of the data structure

    4. D

      They must use O(1) space

    Correct answer
    C

    They must be performed on the same end of the data structure

    Explanation

    In a stack, both push (add) and pop (remove) are done at the same end (the top) to maintain LIFO order.

  31. 31

    What is the maximum number of children a node can have in a binary tree?

    1. A

      unlimited

    2. B

      3

    3. C

      1

    4. D

      2

    Correct answer
    D

    2

    Explanation

    A binary tree is defined as a tree where each node has at most two children, often referred to as left and right child.

  32. 32

    What is a leaf node in a binary tree?

    1. A

      The root node

    2. B

      Any node that is not a leaf

    3. C

      A node with exactly two children

    4. D

      A node with no children

    Correct answer
    D

    A node with no children

    Explanation

    A leaf node (or external node) has no children, meaning both left and right pointers are null.

  33. 33

    What is the height of a binary tree?

    1. A

      The maximum number of edges on a path from the root to a leaf

    2. B

      The number of nodes in the tree

    3. C

      The number of levels

    4. D

      The depth of the root node

    Correct answer
    A

    The maximum number of edges on a path from the root to a leaf

    Explanation

    Height is defined as the longest path from the root to a leaf, measured by the number of edges along that path.

  34. 34

    In a binary tree, what is the depth of the root node?

    1. A

      0

    2. B

      -1

    3. C

      2

    4. D

      1

    Correct answer
    A

    0

    Explanation

    The depth of a node is the number of edges from the root to that node. Since the root is at the top, its depth is 0.

  35. 35

    What is the maximum number of nodes in a binary tree of height hhh?

    1. A

      h2h^2h2

    2. B

      2h2^{h}2h

    3. C

      2h−12^h - 12h−1

    4. D

      2h+1−12^{h+1} - 12h+1−1

    Correct answer
    D

    2h+1−12^{h+1} - 12h+1−1

    Explanation

    A full binary tree of height hhh has the most nodes: 2h+1−12^{h+1} - 12h+1−1 because each level iii (0-indexed) has 2i2^i2i nodes.

  36. 36

    What traversal method uses a queue to process nodes level by level?

    1. A

      Depth-first search

    2. B

      Breadth-first search

    3. C

      Post-order traversal

    4. D

      In-order traversal

    Correct answer
    B

    Breadth-first search

    Explanation

    Breadth-first search (BFS) processes all nodes at the current depth before moving to the next level, typically implemented with a queue.

  37. 37

    In a depth-first traversal of a binary tree, what data structure is used?

    1. A

      Queue

    2. B

      Stack

    3. C

      Array

    4. D

      Linked list

    Correct answer
    B

    Stack

    Explanation

    Depth-first traversal uses a stack (explicitly or via recursion) to go deep into the tree before backtracking.

  38. 38

    What is the order of nodes visited in an in-order traversal?

    1. A

      Right subtree, current node, left subtree

    2. B

      Left subtree, current node, right subtree

    3. C

      Current node, left subtree, right subtree

    4. D

      Left subtree, right subtree, current node

    Correct answer
    B

    Left subtree, current node, right subtree

    Explanation

    In-order traversal processes the left subtree first, then the current node, then the right subtree.

  39. 39

    What defines a full binary tree?

    1. A

      All nodes have either 0 or 2 children

    2. B

      Every node has exactly two children

    3. C

      The tree is balanced

    4. D

      All levels are completely filled

    Correct answer
    A

    All nodes have either 0 or 2 children

    Explanation

    A full binary tree is one where every node has either 0 or 2 children (no node has only one child).

  40. 40

    What defines a complete binary tree?

    1. A

      The height is minimal

    2. B

      All levels are completely filled

    3. C

      Every node has two children

    4. D

      All levels except possibly the last are full, and nodes in the last level are as far left as possible

    Correct answer
    D

    All levels except possibly the last are full, and nodes in the last level are as far left as possible

    Explanation

    A complete binary tree is filled level by level from left to right, with the last level having nodes as left as possible.

  41. 41

    What defines a balanced binary tree?

    1. A

      All leaves are at the same depth

    2. B

      The tree is full

    3. C

      The difference in height between left and right subtrees of any node is at most 1

    4. D

      The root has equal number of nodes on both sides

    Correct answer
    C

    The difference in height between left and right subtrees of any node is at most 1

    Explanation

    A balanced binary tree (height-balanced) ensures that for every node, the height of its left and right subtrees differ by no more than one.

  42. 42

    What is a perfect binary tree?

    1. A

      A tree where the height is minimal

    2. B

      A tree that is both full and complete

    3. C

      A tree where all levels are completely filled

    4. D

      A tree where all nodes have 0 or 2 children

    Correct answer
    C

    A tree where all levels are completely filled

    Explanation

    A perfect binary tree is one in which all internal nodes have two children and all leaves are at the same depth, meaning every level is completely filled.

  43. 43

    In a binary tree, what is the time complexity of inserting a node using level-order (BFS) traversal?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    BFS traversal visits each node until finding the first node with an empty child slot; in the worst case, it may need to visit all nnn nodes, hence O(n)O(n)O(n).

  44. 44

    When deleting a node from a binary tree, why is the deepest rightmost node chosen as the replacement?

    1. A

      It is the root of the tree

    2. B

      It is the largest value in the tree

    3. C

      It is a leaf node and can be easily removed without affecting other children

    4. D

      It is the node with the highest index

    Correct answer
    C

    It is a leaf node and can be easily removed without affecting other children

    Explanation

    The deepest rightmost node is a leaf node (or has at most one child), so swiping its value with the node to delete and then removing it is straightforward and maintains tree structure.

  45. 45

    How many edges are there in a tree with nnn nodes?

    1. A

      nnn

    2. B

      n+1n+1n+1

    3. C

      n−1n-1n−1

    4. D

      2n2n2n

    Correct answer
    C

    n−1n-1n−1

    Explanation

    A tree with nnn nodes always has exactly n−1n-1n−1 edges because it is a connected acyclic graph.

  46. 46

    What is the property of a binary search tree?

    1. A

      For any node, all values in its left subtree are less, and all values in its right subtree are greater than the node's value

    2. B

      All nodes are unique

    3. C

      The tree is balanced

    4. D

      Every node has exactly two children

    Correct answer
    A

    For any node, all values in its left subtree are less, and all values in its right subtree are greater than the node's value

    Explanation

    The BST property ensures that the tree allows efficient searching: left subtree contains smaller values, right subtree contains larger values.

  47. 47

    What is the time complexity of searching for a value in a balanced BST?

    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(nlog⁡n)O(n \log n)O(nlogn)

    Correct answer
    A

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

    Explanation

    In a balanced BST, the height is log⁡2n\log_2 nlog2​n, so each comparison eliminates half the tree, leading to O(log⁡n)O(\log n)O(logn) time.

  48. 48

    What is the worst-case time complexity of searching in a BST?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    D

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

    Explanation

    If the BST becomes skewed (like a linked list), the height becomes nnn, so searching may require visiting all nodes, resulting in O(n)O(n)O(n) time.

  49. 49

    During BST insertion, where is the new node always placed?

    1. A

      As an internal node

    2. B

      As a leaf node

    3. C

      At the root

    4. D

      At a random position

    Correct answer
    B

    As a leaf node

    Explanation

    Insertion always adds the new node as a leaf, found by traversing the tree following the BST property until an empty spot is reached.

  50. 50

    In BST deletion, what is the first case (simplest scenario)?

    1. A

      Deleting a node with one child

    2. B

      Deleting the root node

    3. C

      Deleting a leaf node

    4. D

      Deleting a node with two children

    Correct answer
    C

    Deleting a leaf node

    Explanation

    Deleting a leaf node is simplest: you just remove the reference from its parent, which takes O(1)O(1)O(1) after locating the node.

  51. 51

    In BST deletion, what is the second case (node with one child)?

    1. A

      Use recursion on both subtrees

    2. B

      Swap with the maximum in left subtree

    3. C

      Delete both children

    4. D

      Replace the node with its child by linking parent to child

    Correct answer
    D

    Replace the node with its child by linking parent to child

    Explanation

    If the node has one child, you can bypass it by connecting its parent directly to that child, preserving the BST property.

  52. 52

    In BST deletion, what is the third case (node with two children)?

    1. A

      Delete both children and then the node

    2. B

      Replace the node with a leaf node from its subtree and delete that leaf

    3. C

      Make the left child the new root of the subtree

    4. D

      Randomly choose a child to keep

    Correct answer
    B

    Replace the node with a leaf node from its subtree and delete that leaf

    Explanation

    For two children, you find either the maximum in the left subtree or the minimum in the right subtree, copy its value to the node, then delete that leaf (or one-child) node.

  53. 53

    When deleting a node with two children, why is the maximum of the left subtree chosen as the replacement?

    1. A

      It is randomly selected

    2. B

      It is the largest value smaller than the node, preserving BST property

    3. C

      It is the leftmost node

    4. D

      It is the node with the smallest value in the left subtree

    Correct answer
    B

    It is the largest value smaller than the node, preserving BST property

    Explanation

    The maximum in the left subtree is the greatest value that is still less than the node's value, so replacing keeps the BST order intact.

  54. 54

    What does the height of a BST depend on?

    1. A

      The size of the elements

    2. B

      The data type of the elements

    3. C

      The programming language used

    4. D

      The order in which elements are inserted

    Correct answer
    D

    The order in which elements are inserted

    Explanation

    If elements are inserted in sorted order, the tree becomes skewed (height nnn); if inserted in random order, height tends to O(log⁡n)O(\log n)O(logn).

  55. 55

    What is a self-balancing BST?

    1. A

      A BST that only allows leaf deletions

    2. B

      A BST that uses a hash function

    3. C

      A BST that stores data in an array

    4. D

      A BST that automatically maintains a balanced height after insertions and deletions

    Correct answer
    D

    A BST that automatically maintains a balanced height after insertions and deletions

    Explanation

    Self-balancing BSTs (e.g., AVL trees, Red-Black trees) perform rotations or adjustments to keep the tree height O(log⁡n)O(\log n)O(logn).

  56. 56

    What is a max-heap property?

    1. A

      Every parent node is less than or equal to its children

    2. B

      The tree is balanced

    3. C

      Every parent node is greater than or equal to its children

    4. D

      The root is the smallest element

    Correct answer
    C

    Every parent node is greater than or equal to its children

    Explanation

    In a max-heap, the value of each node is at least as large as the values of its children, so the maximum is at the root.

  57. 57

    What data structure is commonly used to implement a heap?

    1. A

      Graph

    2. B

      Linked list

    3. C

      Queue

    4. D

      Array

    Correct answer
    D

    Array

    Explanation

    Heaps are efficiently implemented using arrays because the complete binary tree structure allows easy calculation of parent and child indices.

  58. 58

    In a heap represented as an array, how do you find the left child of a node at index iii?

    1. A

      2i+22i+22i+2

    2. B

      i−1i-1i−1

    3. C

      i/2i/2i/2

    4. D

      2i+12i+12i+1

    Correct answer
    D

    2i+12i+12i+1

    Explanation

    For a zero-indexed array, the left child of node at index iii is at index 2i+12i+12i+1 (if within bounds).

  59. 59

    In a heap represented as an array, how do you find the parent of a node at index iii?

    1. A

      ⌊i/2⌋\lfloor i/2 \rfloor⌊i/2⌋

    2. B

      i−1i-1i−1

    3. C

      ⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor⌊(i−1)/2⌋

    4. D

      2i2i2i

    Correct answer
    C

    ⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor⌊(i−1)/2⌋

    Explanation

    The parent index is found by ⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor⌊(i−1)/2⌋, which for zero-indexed arrays gives the correct parent.

  60. 60

    What is the time complexity of inserting a new element into a heap?

    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(nlog⁡n)O(n \log n)O(nlogn)

    Correct answer
    A

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

    Explanation

    Insertion adds the element at the end and then sifts it up (bubbles up) to restore heap property. The number of swaps is at most the height, O(log⁡n)O(\log n)O(logn).

  61. 61

    What operation is performed after inserting at the end of a heap to restore the heap property?

    1. A

      sift-down

    2. B

      heapify

    3. C

      reheap

    4. D

      sift-up

    Correct answer
    D

    sift-up

    Explanation

    After adding a new element at the last position, we sift it up (or bubble up) by swapping with its parent until the heap property is satisfied.

  62. 62

    What is the time complexity of extracting the maximum element from a max-heap?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Extracting the max involves replacing the root with the last element and then sifting down, which takes O(log⁡n)O(\log n)O(logn) time.

  63. 63

    After extracting the maximum from a max-heap, what is typically done to the heap?

    1. A

      The root is deleted and the heap remains unchanged

    2. B

      The heap is rebuilt from scratch

    3. C

      All elements are shifted left

    4. D

      The last element is moved to the root and then sifted down

    Correct answer
    D

    The last element is moved to the root and then sifted down

    Explanation

    To maintain the complete tree, the last element is placed at the root, then sifted down to restore the heap property.

  64. 64

    How is a heap built from an unsorted array of nnn elements?

    1. A

      By repeatedly inserting each element into an empty heap, which takes O(nlog⁡n)O(n \log n)O(nlogn) time

    2. B

      By sorting the array first

    3. C

      By dividing and conquering

    4. D

      By using a binary search tree

    Correct answer
    A

    By repeatedly inserting each element into an empty heap, which takes O(nlog⁡n)O(n \log n)O(nlogn) time

    Explanation

    One common method is to start with an empty heap and insert each element one by one, each insertion costing O(log⁡n)O(\log n)O(logn), totaling O(nlog⁡n)O(n \log n)O(nlogn).

  65. 65

    What is heap sort?

    1. A

      A sorting algorithm that uses binary search

    2. B

      A sorting algorithm that compares adjacent elements

    3. C

      A sorting algorithm that divides the array into halves

    4. D

      A sorting algorithm that builds a heap and repeatedly extracts the maximum/minimum element

    Correct answer
    D

    A sorting algorithm that builds a heap and repeatedly extracts the maximum/minimum element

    Explanation

    Heap sort first builds a heap from the input, then repeatedly removes the root (max or min) and inserts it into the sorted output, achieving O(nlog⁡n)O(n \log n)O(nlogn).

  66. 66

    What is the time complexity of heap sort?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    B

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

    Explanation

    Heap sort consists of building a heap (O(nlog⁡n)O(n \log n)O(nlogn)) and then nnn extractions (O(nlog⁡n)O(n \log n)O(nlogn)), so total is O(nlog⁡n)O(n \log n)O(nlogn).

  67. 67

    In a min-heap, where is the minimum element located?

    1. A

      At a leaf

    2. B

      At the last index

    3. C

      It could be anywhere

    4. D

      At the root

    Correct answer
    D

    At the root

    Explanation

    By definition, in a min-heap every parent is less than or equal to its children, so the smallest element is always at the root.

  68. 68

    What is a vertex in a graph?

    1. A

      A node or point in the graph

    2. B

      A cycle

    3. C

      A connection between two nodes

    4. D

      A path between nodes

    Correct answer
    A

    A node or point in the graph

    Explanation

    Vertices (or nodes) are the fundamental units of a graph that can be connected by edges.

  69. 69

    What is an edge in a graph?

    1. A

      A traversal algorithm

    2. B

      A connection between two vertices

    3. C

      A node in the graph

    4. D

      A data structure

    Correct answer
    B

    A connection between two vertices

    Explanation

    Edges represent relationships or connections between pairs of vertices.

  70. 70

    What is an undirected graph?

    1. A

      A graph without weights

    2. B

      A graph where edges have no direction

    3. C

      A graph with cycles

    4. D

      A graph where edges have arrows

    Correct answer
    B

    A graph where edges have no direction

    Explanation

    In an undirected graph, edges are bidirectional; the pair (u,v) is the same as (v,u).

  71. 71

    What is a directed graph?

    1. A

      A graph where edges have a direction

    2. B

      A graph where all vertices are connected

    3. C

      A graph with weights on edges

    4. D

      A graph that is a tree

    Correct answer
    A

    A graph where edges have a direction

    Explanation

    In a directed graph (digraph), each edge has a specific direction from one vertex to another.

  72. 72

    What is a weighted graph?

    1. A

      A graph that is balanced

    2. B

      A graph where edges have associated numeric values

    3. C

      A graph with many vertices

    4. D

      A graph with no cycles

    Correct answer
    B

    A graph where edges have associated numeric values

    Explanation

    Weighted graphs assign a weight (cost, length, etc.) to each edge, often used for optimization problems.

  73. 73

    What is a cycle in a directed graph?

    1. A

      A path with no repeated vertices

    2. B

      A set of edges that form a loop

    3. C

      A path that visits every vertex exactly once

    4. D

      A path that starts and ends at the same vertex following directed edges

    Correct answer
    D

    A path that starts and ends at the same vertex following directed edges

    Explanation

    A cycle is a sequence of vertices where the first and last are the same, and each consecutive pair is connected by a directed edge.

  74. 74

    What is an acyclic graph?

    1. A

      A graph that contains no cycles

    2. B

      A graph that is disconnected

    3. C

      A graph that has only one path between vertices

    4. D

      A graph that is complete

    Correct answer
    A

    A graph that contains no cycles

    Explanation

    An acyclic graph (e.g., a tree or DAG) has no cycles, meaning there is no way to return to a vertex by following edges.

  75. 75

    What is an adjacency matrix?

    1. A

      A 2D array where cell (i,j) indicates the presence or weight of an edge from vertex i to j

    2. B

      A hash table of vertices

    3. C

      A list of edges

    4. D

      A tree representation

    Correct answer
    A

    A 2D array where cell (i,j) indicates the presence or weight of an edge from vertex i to j

    Explanation

    An adjacency matrix is a square matrix of size V×VV \times VV×V where entry aija_{ij}aij​ represents the edge between vertex iii and jjj.

  76. 76

    What is an adjacency list?

    1. A

      A hash set of edges

    2. B

      A linked list of vertices

    3. C

      A collection of lists where each vertex has a list of its adjacent vertices

    4. D

      A 2D array of edges

    Correct answer
    C

    A collection of lists where each vertex has a list of its adjacent vertices

    Explanation

    Adjacency list uses an array of lists; for each vertex, a list stores the vertices it is connected to (and possibly edge weights).

  77. 77

    Which representation is more space-efficient for a sparse graph?

    1. A

      Adjacency matrix

    2. B

      Both are equally efficient

    3. C

      Incidence matrix

    4. D

      Adjacency list

    Correct answer
    D

    Adjacency list

    Explanation

    Adjacency list uses O(V+E)O(V+E)O(V+E) space, while adjacency matrix uses O(V2)O(V^2)O(V2) space, making the list more efficient for sparse graphs.

  78. 78

    Which representation allows O(1)O(1)O(1) edge existence check?

    1. A

      Adjacency matrix

    2. B

      Adjacency list

    3. C

      Both

    4. D

      Neither

    Correct answer
    A

    Adjacency matrix

    Explanation

    In an adjacency matrix, checking if edge (i,j) exists is done by accessing cell (i,j) in O(1)O(1)O(1) time, whereas adjacency list may require traversal.

  79. 79

    What is a hash function?

    1. A

      A function that compresses data

    2. B

      A function that maps input data to a fixed-size output, usually an integer

    3. C

      A function that sorts data

    4. D

      A function that encrypts data

    Correct answer
    B

    A function that maps input data to a fixed-size output, usually an integer

    Explanation

    A hash function takes an input (key) and returns a fixed-size value, often used to index into a hash table.

  80. 80

    What is a hash collision?

    1. A

      When the hash table is full

    2. B

      When the hash function runs out of memory

    3. C

      When two different inputs produce the same hash output

    4. D

      When the hash output is too large

    Correct answer
    C

    When two different inputs produce the same hash output

    Explanation

    A collision occurs when the hash function maps two distinct keys to the same index, requiring a resolution strategy.

  81. 81

    Why is it desirable for a hash function to produce unique outputs?

    1. A

      To reduce memory usage

    2. B

      To avoid collisions and maintain O(1)O(1)O(1) average operation time

    3. C

      To ensure security

    4. D

      To make the function faster

    Correct answer
    B

    To avoid collisions and maintain O(1)O(1)O(1) average operation time

    Explanation

    Unique outputs prevent collisions, which would otherwise degrade performance due to resolution overhead, keeping lookups O(1)O(1)O(1) on average.

  82. 82

    How does hashing enable constant-time lookup in a hash table?

    1. A

      The hash table uses binary search

    2. B

      The hash function computes an index where the key-value pair is stored, allowing direct access

    3. C

      The hash table uses linked lists

    4. D

      The hash function sorts the keys

    Correct answer
    B

    The hash function computes an index where the key-value pair is stored, allowing direct access

    Explanation

    The hash function generates an index directly from the key, so the table can go to that index in O(1)O(1)O(1) time to retrieve the value.

  83. 83

    What common data structure uses hashing?

    1. A

      Linked list

    2. B

      Queue

    3. C

      Hash map (dictionary)

    4. D

      Stack

    Correct answer
    C

    Hash map (dictionary)

    Explanation

    Hash maps (also called hash tables or dictionaries) rely on hash functions to map keys to indices for efficient storage and retrieval.

  84. 84

    What happens when a hash collision occurs?

    1. A

      A collision resolution strategy is used, such as chaining or open addressing

    2. B

      The hash function is recalculated

    3. C

      The hash table crashes

    4. D

      The new element is discarded

    Correct answer
    A

    A collision resolution strategy is used, such as chaining or open addressing

    Explanation

    Collisions are handled by techniques like chaining (linked list per bucket) or open addressing (probing for next free slot) to store multiple items at the same index.

  85. 85

    What is a perfect hash function?

    1. A

      A hash function that uses the least memory

    2. B

      A hash function that is fastest to compute

    3. C

      A hash function that maps each input to a unique output with no collisions

    4. D

      A hash function that works for any input size

    Correct answer
    C

    A hash function that maps each input to a unique output with no collisions

    Explanation

    A perfect hash function guarantees that different keys map to distinct indices, eliminating collisions entirely for a given set of keys.

  86. 86

    In practice, why is it difficult to achieve perfect hashing?

    1. A

      Because the input set is often unknown or dynamic, and the output range is limited

    2. B

      Because hash functions are not deterministic

    3. C

      Because hash functions are too fast

    4. D

      Because computers have limited processing power

    Correct answer
    A

    Because the input set is often unknown or dynamic, and the output range is limited

    Explanation

    Perfect hashing requires knowledge of all keys in advance and a large enough range; for dynamic or large sets, collisions are inevitable.

  87. 87

    What is a digest in the context of hashing?

    1. A

      The input to a hash function

    2. B

      A type of hash table

    3. C

      A collision resolution method

    4. D

      The output of a hash function

    Correct answer
    D

    The output of a hash function

    Explanation

    Digest refers to the fixed-length result produced by a hash function from an input message.

Question 1 of 87
Question 1 of 87

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent