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

  • 48Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Big O Notation

What is the purpose of Big O notation?

48 questions

  1. 1

    What is the purpose of Big O notation?

    Model answer

    Big O notation describes the performance of an algorithm and helps determine if it scales well as the input grows larger.

    Explanation

    Big O is used to express the runtime complexity of an algorithm, focusing on how the execution time increases with input size, ignoring constants and lower-order terms.

  2. 2

    Why is Big O notation important in coding interviews?

    Model answer

    Companies like Google, Microsoft, and Amazon ask Big O questions to see if you understand how scalable an algorithm is.

    Explanation

    Knowing Big O allows you to analyze and compare algorithms' efficiency, which is a key skill for software engineering roles.

  3. 3

    What does O(1) represent in Big O notation?

    Model answer

    O(1) represents constant time complexity, meaning the algorithm takes the same amount of time regardless of input size.

    Explanation

    For example, accessing an array element by index or printing the first item is O(1) because it involves a single operation independent of the array length.

  4. 4

    What is the runtime complexity of a method that prints the first element of an array?

    Model answer

    O(1) – constant time.

    Explanation

    No matter how large the array is, only one print operation is executed.

  5. 5

    What is the runtime complexity of a single loop that iterates over all items in an array?

    Model answer

    O(n) – linear time, where n is the size of the array.

    Explanation

    The number of iterations grows linearly with the input size; for n items, there are n operations.

  6. 6

    How do you simplify Big O expressions like O(1 + n + 1)?

    Model answer

    Drop the constants to get O(n), because as n grows large, the constant terms become negligible.

    Explanation

    Big O is about growth rate approximation; O(2 + n) simplifies to O(n) since the linear term dominates.

  7. 7

    What is the runtime complexity of two consecutive loops over the same array?

    Model answer

    O(n) – after dropping the constant 2, it remains linear.

    Explanation

    O(n + n) = O(2n), simplified to O(n).

  8. 8

    How do you express runtime complexity when an algorithm has two different input sizes?

    Model answer

    Use different variables, e.g., O(n + m), where n and m are the sizes of the two inputs.

    Explanation

    This distinguishes the contributions of each input; they can be simplified to O(n) if they both grow linearly, but using separate variables is more precise.

  9. 9

    What is the runtime complexity of nested loops over the same array?

    Model answer

    O(n^2) – quadratic time.

    Explanation

    The outer loop runs n times, and for each iteration, the inner loop runs n times, yielding n * n = n^2 operations.

  10. 10

    What is the runtime complexity of an algorithm that reduces the work by half in each step?

    Model answer

    O(log n) – logarithmic time.

    Explanation

    Binary search is a classic example: with each comparison, the search space is halved, requiring at most log₂(n) steps.

  11. 11

    Give an example of an O(log n) algorithm. Why is it efficient?

    Model answer

    Binary search on a sorted array runs in O(log n); it is efficient because the number of operations grows much slower than the input size.

    Explanation

    For 1 million items, binary search takes at most 19 comparisons, while linear search could take 1 million.

  12. 12

    Compare logarithmic growth and exponential growth.

    Model answer

    Logarithmic growth slows down as input increases, while exponential growth accelerates rapidly.

    Explanation

    O(log n) algorithms are highly scalable; O(2^n) algorithms become unusable for large inputs.

  13. 13

    What is space complexity, and how is it analyzed?

    Model answer

    Space complexity measures the additional memory an algorithm allocates relative to the input size, not counting the input itself.

    Explanation

    We only consider extra space; e.g., creating a copy array of size n gives O(n) space complexity.

  14. 14

    What is the space complexity of a method that only declares a local loop variable?

    Model answer

    O(1) – constant space.

    Explanation

    The loop variable uses a fixed amount of memory regardless of input size.

  15. 15

    What is the space complexity of creating a new array the same size as the input?

    Model answer

    O(n) – linear space.

    Explanation

    The new array's size grows directly with the input size.

  16. 16

    Why is Big O analysis focused on the worst-case scenario?

    Model answer

    We consider the worst-case because it gives an upper bound on the algorithm's performance and ensures scalability.

    Explanation

    For example, linear search can be O(1) if the target is first, but worst-case O(n) when it's last; we always use the worst-case for guarantees.

  17. 17

    How does Big O notation help in choosing between data structures?

    Model answer

    Different operations have different costs on different data structures; Big O quantifies these costs and guides selection.

    Explanation

    Arrays have O(1) access but O(n) insertion/deletion; linked lists have O(1) insert/delete at ends but O(n) access.

  18. 18

    Describe how array elements are stored in memory and why index lookup is fast.

    Model answer

    Array elements are stored sequentially in contiguous memory; given the base address and element size, the address of any index can be computed in constant time.

    Explanation

    For example, an integer array at base address 100 has element at index i at address 100 + i*4 (since int is 4 bytes). This arithmetical calculation is O(1).

  19. 19

    What is the main limitation of arrays in Java?

    Model answer

    Arrays have a fixed size that must be specified at allocation and cannot be changed later.

    Explanation

    This static nature means you must know the size ahead of time; if you guess too large, you waste memory; too small, you need to resize (O(n) operation).

  20. 20

    What is the runtime complexity of resizing an array (copying all elements to a new array)?

    Model answer

    O(n) – linear time.

    Explanation

    Each of the n elements must be copied to the new array, so the cost grows linearly with the number of elements.

  21. 21

    What is the worst-case runtime complexity of inserting an element at the beginning of an array?

    Model answer

    O(n) – because all existing elements must be shifted to the right.

    Explanation

    In the worst case, inserting at index 0 requires moving each element one position, an O(n) operation.

  22. 22

    What is the runtime complexity of deleting the last element of an array?

    Model answer

    O(1) – constant time.

    Explanation

    Simply clear the last cell; no shifting is required.

  23. 23

    What is the runtime complexity of deleting an element from the beginning of an array?

    Model answer

    O(n) – because all remaining elements must be shifted left.

    Explanation

    After removing the first element, every element after it must be moved one position to fill the gap.

  24. 24

    By what percentage does an ArrayList grow when it becomes full in Java?

    Model answer

    50% (grows by half of its current size).

    Explanation

    The ArrayList increases its capacity by 50% (e.g., from 10 to 15), while the Vector class grows by 100%.

  25. 25

    What is the difference between Vector and ArrayList regarding thread safety?

    Model answer

    Vector methods are synchronized (thread-safe), while ArrayList methods are not synchronized.

    Explanation

    In multi-threaded applications, synchronized methods can be a bottleneck; ArrayList is preferred for single-threaded use for better performance.

  26. 26

    In Java's ArrayList, which method returns the number of elements?

    Model answer

    size().

    Explanation

    ArrayList has a size() method (not length) that returns the count of elements stored.

  27. 27

    How can you convert an ArrayList to a regular array in Java?

    Model answer

    Using the toArray() method.

    Explanation

    For example, list.toArray() returns an Object[]; you can also pass an array of the desired type.

  28. 28

    In the custom Array class built in the tutorial, what field keeps track of the number of items?

    Model answer

    A private int called count.

    Explanation

    count is incremented on insertion and decremented on removal; it represents actual number of elements, distinct from the capacity.

  29. 29

    In the custom Array class, what is the worst-case runtime complexity of the indexOf method?

    Model answer

    O(n) – linear time.

    Explanation

    In the worst case, the target item is at the end, requiring a full traversal of all elements.

  30. 30

    What exception does the custom Array class throw when removing an item with an out-of-range index?

    Model answer

    IllegalArgumentException.

    Explanation

    This is a programming error; the exception signals that the index is invalid (negative or >= count).

  31. 31

    In the custom Array class, how is the array resized when full?

    Model answer

    A new array of twice the size is created, all existing items are copied, and the items field is updated.

    Explanation

    The new size is count * 2; the loop copies each element from the old array to the new one.

  32. 32

    What are the two pieces of data stored in a linked list node?

    Model answer

    A value (e.g., an integer) and a reference (pointer) to the next node.

    Explanation

    The next field points to the subsequent node in the sequence; the last node's next is null.

  33. 33

    What do head and tail represent in a linked list?

    Model answer

    Head is the first node; tail is the last node.

    Explanation

    These references point to the beginning and end of the list, enabling efficient operations at both ends.

  34. 34

    Why is accessing an element by index in a linked list O(n) while in an array it is O(1)?

    Model answer

    Array elements are stored contiguously, so index maps directly to memory address; linked list nodes are scattered, requiring traversal from head to find the nth node.

    Explanation

    Each node only knows its next neighbor; to reach index i, you must follow i links.

  35. 35

    What is the runtime complexity of searching for a value in a linked list?

    Model answer

    O(n) – linear time.

    Explanation

    In the worst case, the value is at the tail, requiring a full traversal.

  36. 36

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

    Model answer

    O(1) – constant time.

    Explanation

    Create a new node, set its next to current head, then update head to the new node. No traversal needed.

  37. 37

    What is the runtime complexity of inserting a new node at the end of a linked list (assuming you have a tail reference)?

    Model answer

    O(1) – constant time.

    Explanation

    Set tail.next to new node, then update tail to new node. The tail reference allows direct access to the end.

  38. 38

    What is the runtime complexity of inserting a node in the middle of a linked list?

    Model answer

    O(n) – to find the insertion point, then O(1) to adjust links.

    Explanation

    You must traverse from head until you reach the desired position; the actual link update is constant time.

  39. 39

    What is the runtime complexity of deleting the first node of a linked list?

    Model answer

    O(1) – constant time.

    Explanation

    Set head to head.next; optionally remove the old head's link to avoid memory leaks.

  40. 40

    What is the runtime complexity of deleting the last node of a linked list?

    Model answer

    O(n) – because you need to find the node before the tail.

    Explanation

    From head, traverse until you reach a node whose next is the tail; then update tail and set the previous node's next to null.

  41. 41

    What is the runtime complexity of deleting a node from the middle of a linked list?

    Model answer

    O(n) – to find the node and its predecessor, then O(1) to adjust links.

    Explanation

    You must traverse to the node; the deletion itself is constant after locating it.

  42. 42

    In Java's LinkedList class, which method adds an item at the beginning?

    Model answer

    addFirst(E element).

    Explanation

    LinkedList provides addFirst, addLast, removeFirst, removeLast, etc.

  43. 43

    In Java's LinkedList class, which method removes the last item?

    Model answer

    removeLast().

    Explanation

    It removes and returns the last element; throws NoSuchElementException if list is empty.

  44. 44

    In the custom LinkedList implementation, what is the purpose of the is_empty method?

    Model answer

    To check if the list has no nodes by returning whether first == null.

    Explanation

    This private method encapsulates the check and is used in other operations to avoid invalid actions.

  45. 45

    In the custom LinkedList implementation, what must be done when removing the first node from a list with a single element?

    Model answer

    Set both first and last to null and return.

    Explanation

    If first == last (only one node), after removal the list becomes empty; both references must be updated.

  46. 46

    In the custom LinkedList implementation, how do you find the node before the last node?

    Model answer

    Traverse from head until you find a node whose next is the tail.

    Explanation

    A helper method getPrevious(Node) is used; it returns the node that points to the given node, or null if not found.

  47. 47

    What exception does the custom LinkedList class throw when trying to remove from an empty list?

    Model answer

    NoSuchElementException.

    Explanation

    This is consistent with Java's built-in LinkedList behavior, preventing a null pointer exception.

  48. 48

    In the custom LinkedList's removeLast method, what is the purpose of unlinking the old tail?

    Model answer

    To allow the garbage collector to reclaim the removed node's memory.

    Explanation

    After updating tail to the previous node, we set tail.next = null so no reference points to the old tail.

1 of 48
Question 1 of 48

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent