1. Home
  2. Explore
  3. Computer Science
  4. Data Structures & Algorithms Basics

Study methods

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

Data Structures & Algorithms Basics

  • 34Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Big O Basics

What is the primary purpose of Big O notation in computer science?

34 questions

  1. 1

    What is the primary purpose of Big O notation in computer science?

    1. A

      To measure the memory usage of a program

    2. B

      To compare the syntax of different programming languages

    3. C

      To describe the performance and scalability of an algorithm as input grows

    4. D

      To describe the exact execution time of an algorithm in milliseconds

    Correct answer
    C

    To describe the performance and scalability of an algorithm as input grows

    Explanation

    Big O notation describes how the runtime or space requirements of an algorithm grow as the input size increases, helping determine scalability.

  2. 2

    Why is Big O notation important when choosing data structures?

    1. A

      It determines the exact number of lines of code needed

    2. B

      It specifies the programming language to use

    3. C

      It helps assess how costly operations become with large inputs

    4. D

      It ensures the code compiles correctly

    Correct answer
    C

    It helps assess how costly operations become with large inputs

    Explanation

    Different data structures have different time complexities for operations; Big O helps evaluate which structure performs better for large datasets.

  3. 3

    Which of the following best describes the runtime complexity of a simple loop that iterates over all elements of an 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

    A single loop that processes each element once has linear growth — the number of operations is directly proportional to nnn.

  4. 4

    If an algorithm has runtime O(n+100)O(n + 100)O(n+100), how can it be simplified?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(n+100)O(n + 100)O(n+100)

    Correct answer
    C

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

    Explanation

    Constants are dropped in Big O notation because they do not affect the growth rate as nnn becomes large; O(n+100)O(n+100)O(n+100) simplifies to O(n)O(n)O(n).

  5. 5

    What is the runtime complexity of a method that contains two consecutive loops, each iterating over the same input of size nnn?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Two consecutive loops still represent linear growth; O(2n)O(2n)O(2n) simplifies to O(n)O(n)O(n) because constants are dropped.

  6. 6

    If an algorithm processes two different inputs of sizes nnn and mmm with separate loops, what is the simplified runtime complexity?

    1. A

      O(nƗm)O(n \times m)O(nƗm)

    2. B

      O(n+m)O(n + m)O(n+m)

    3. C

      O(max⁔(n,m))O(\max(n, m))O(max(n,m))

    4. D

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

    Correct answer
    B

    O(n+m)O(n + m)O(n+m)

    Explanation

    With two independent inputs, the runtime is O(n+m)O(n + m)O(n+m), which is linear in the total input size.

  7. 7

    What is the runtime complexity of a method with a loop that runs nnn times and, inside it, another loop that also runs nnn times?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Nested loops multiply the iterations: O(nƗn)=O(n2)O(n \times n) = O(n^2)O(nƗn)=O(n2), quadratic time.

  8. 8

    If an algorithm runs in O(n+n2)O(n + n^2)O(n+n2), which term dominates and what is the simplified complexity?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(n3)O(n^3)O(n3)

    Correct answer
    B

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

    Explanation

    The n2n^2n2 term grows faster than nnn, so the lower-order term is dropped, simplifying to O(n2)O(n^2)O(n2).

  9. 9

    Which searching algorithm runs in O(log⁔n)O(\log n)O(logn) time on a sorted array?

    1. A

      Linear search

    2. B

      Insertion sort

    3. C

      Bubble sort

    4. D

      Binary search

    Correct answer
    D

    Binary search

    Explanation

    Binary search repeatedly divides the search interval in half, achieving logarithmic time complexity.

  10. 10

    In binary search, how many comparisons are needed at most to find an element in a sorted array of 1 million items?

    1. A

      20

    2. B

      1 million

    3. C

      500,000

    4. D

      19

    Correct answer
    D

    19

    Explanation

    Binary search requires at most log⁔2(1,000,000)ā‰ˆ20\log_2(1,000,000) \approx 20log2​(1,000,000)ā‰ˆ20 comparisons, so 19 or 20; the video states 19.

  11. 11

    How does the runtime of an O(log⁔n)O(\log n)O(logn) algorithm compare to an O(n)O(n)O(n) algorithm as input grows?

    1. A

      It remains constant

    2. B

      It grows faster

    3. C

      It grows at the same rate

    4. D

      It grows more slowly

    Correct answer
    D

    It grows more slowly

    Explanation

    Logarithmic growth slows down over time, making it more scalable than linear growth.

  12. 12

    Which growth rate is considered the opposite of logarithmic growth?

    1. A

      Exponential growth

    2. B

      Quadratic growth

    3. C

      Constant growth

    4. D

      Linear growth

    Correct answer
    A

    Exponential growth

    Explanation

    Exponential growth (O(2n)O(2^n)O(2n)) is the inverse of logarithmic growth — it increases faster and faster.

  13. 13

    What is the space complexity of a method that declares a single integer variable regardless of input size?

    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

    A fixed number of variables uses constant space, O(1)O(1)O(1), independent of input size.

  14. 14

    When analyzing space complexity, what do we consider?

    1. A

      Only the additional memory allocated by the algorithm

    2. B

      Total memory used including input

    3. C

      The memory used by the operating system

    4. D

      Only the memory used by the input

    Correct answer
    A

    Only the additional memory allocated by the algorithm

    Explanation

    Space complexity focuses on extra memory needed beyond the input, because the input size is already given.

  15. 15

    How are array elements stored in memory?

    1. A

      In a linked structure with pointers

    2. B

      Contiguously in sequential memory locations

    3. C

      In a binary tree format

    4. D

      Randomly scattered

    Correct answer
    B

    Contiguously in sequential memory locations

    Explanation

    Arrays store elements in contiguous memory blocks, allowing direct index-based access.

  16. 16

    What is the runtime complexity of accessing an array element by its index?

    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

    Accessing an array element by index is a simple address calculation that runs in constant time.

  17. 17

    What is a major limitation of arrays in Java?

    1. A

      They can only store integers

    2. B

      They cannot be iterated using loops

    3. C

      They have a fixed size that cannot change

    4. D

      They store elements in non-contiguous memory

    Correct answer
    C

    They have a fixed size that cannot change

    Explanation

    Arrays are static; once created, their size cannot be altered without creating a new array.

  18. 18

    What is the runtime complexity of resizing an array that contains nnn elements?

    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

    Resizing requires allocating a new array and copying all existing elements, which takes linear time.

  19. 19

    What is the worst-case runtime complexity of removing an element from the beginning of an array?

    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
    D

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

    Explanation

    Removing from the start requires shifting all remaining elements left, which is O(n)O(n)O(n).

  20. 20

    When building a dynamic array class, what field keeps track of the number of items actually stored?

    1. A

      capacity

    2. B

      count

    3. C

      items.length

    4. D

      size

    Correct answer
    B

    count

    Explanation

    The count field tracks the number of items inserted, while items.length represents the allocated capacity.

  21. 21

    In the custom dynamic array implementation, how is the array resized when full?

    1. A

      Increase by 50%

    2. B

      Triple the size

    3. C

      Increase by a fixed number (e.g., 10)

    4. D

      Double the size

    Correct answer
    D

    Double the size

    Explanation

    The video doubles the array size: new int[count * 2].

  22. 22

    What is the runtime complexity of the indexOf method in the custom array (worst case)?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(log⁔n)O(\log n)O(logn)

    Correct answer
    A

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

    Explanation

    In the worst case (item at the end or not found), the method traverses all elements, so O(n)O(n)O(n).

  23. 23

    What does the removeAt method do after shifting elements?

    1. A

      Decrements the count

    2. B

      Sets the last element to null

    3. C

      Returns the removed value

    4. D

      Increases the array size

    Correct answer
    A

    Decrements the count

    Explanation

    After shifting, the count is decremented to reflect the reduced number of items.

  24. 24

    What are the two Java classes that implement dynamic arrays, and how do they differ?

    1. A

      ArrayList and LinkedList; both grow by 100%

    2. B

      Vector and ArrayList; Vector grows by 50% and is synchronized, ArrayList grows by 100% and is not synchronized

    3. C

      Vector and ArrayList; Vector grows by 100% and is synchronized, ArrayList grows by 50% and is not synchronized

    4. D

      Vector and LinkedList; Vector is thread-safe, LinkedList is not

    Correct answer
    C

    Vector and ArrayList; Vector grows by 100% and is synchronized, ArrayList grows by 50% and is not synchronized

    Explanation

    Vector doubles its size and synchronizes methods; ArrayList grows by 50% and methods are not synchronized.

  25. 25

    Which method converts an ArrayList to a regular array?

    1. A

      asArray()

    2. B

      toList()

    3. C

      toArray()

    4. D

      getArray()

    Correct answer
    C

    toArray()

    Explanation

    The toArray() method returns an array containing all elements of the ArrayList.

  26. 26

    In a linked list, what does each node contain?

    1. A

      Only a value

    2. B

      A value and an index

    3. C

      A value and a pointer to the previous node

    4. D

      A value and a pointer to the next node

    Correct answer
    D

    A value and a pointer to the next node

    Explanation

    Each node stores a value and a reference (pointer) to the next node in the sequence.

  27. 27

    What are the first and last nodes of a linked list called?

    1. A

      Start and End

    2. B

      First and Last

    3. C

      Front and Back

    4. D

      Head and Tail

    Correct answer
    B

    First and Last

    Explanation

    The video uses first and last (equivalent to head and tail) for the first and last nodes.

  28. 28

    What is the runtime complexity of inserting a new node at the beginning of a linked list?

    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

    Inserting at the beginning only requires creating a node and updating the head pointer — constant time.

  29. 29

    What is the runtime complexity of inserting a node at the end of a linked list when we have a tail reference?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(log⁔n)O(\log n)O(logn)

    Correct answer
    C

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

    Explanation

    With a tail reference, adding at the end is O(1)O(1)O(1) because we directly access the last node.

  30. 30

    What is the worst-case runtime complexity of deleting the last node in a singly linked list?

    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

    Deleting the last node requires finding the second-to-last node by traversing from the head, which is O(n)O(n)O(n).

  31. 31

    Why must we remove the link from the previous head when deleting the first node?

    1. A

      To allow Java's garbage collector to reclaim the removed node

    2. B

      To update the tail pointer

    3. C

      To avoid memory leaks in C++

    4. D

      To maintain the list's sorted order

    Correct answer
    A

    To allow Java's garbage collector to reclaim the removed node

    Explanation

    Java's garbage collector only removes objects with no references; unlinking ensures the old head can be collected.

  32. 32

    In the custom linked list implementation, what is the purpose of the getPrevious method?

    1. A

      To return the last node

    2. B

      To return the first node

    3. C

      To check if the list contains a value

    4. D

      To return the previous node of any given node

    Correct answer
    D

    To return the previous node of any given node

    Explanation

    getPrevious traverses the list to find the node immediately before a specified node, used in removeLast.

  33. 33

    What exception is thrown when attempting to remove an element from an empty linked list (as per the custom implementation)?

    1. A

      NoSuchElementException

    2. B

      IndexOutOfBoundsException

    3. C

      IllegalArgumentException

    4. D

      NullPointerException

    Correct answer
    A

    NoSuchElementException

    Explanation

    The NoSuchElementException is thrown to indicate the operation is invalid on an empty list.

  34. 34

    In the custom linked list's addLast method, what happens when the list is empty?

    1. A

      Only first is set to the new node

    2. B

      An exception is thrown

    3. C

      Only last is set to the new node

    4. D

      Both first and last are set to the new node

    Correct answer
    D

    Both first and last are set to the new node

    Explanation

    When the list is empty, the new node becomes both the first and last node.

Question 1 of 34
Question 1 of 34

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent