1. Home
  2. Explore
  3. Computer Science
  4. Top 7 Coding Interview Algorithms

Study methods

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

Top 7 Coding Interview Algorithms

  • 33Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Search Algorithms

Binary search requires the list to be:

33 questions

  1. 1

    Binary search requires the list to be:

    1. A

      Sorted in ascending or descending order

    2. B

      Containing unique elements

    3. C

      Stored in an array

    4. D

      Unsorted

    Correct answer
    A

    Sorted in ascending or descending order

    Explanation

    Binary search only works on sorted lists because it relies on comparing the target to the middle element to eliminate half of the list.

  2. 2

    The time complexity of binary search in the worst case is:

    1. A

      O(n)

    2. B

      O(log n)

    3. C

      O(n^2)

    4. D

      O(n log n)

    Correct answer
    B

    O(log n)

    Explanation

    Binary search divides the search space in half each time, resulting in O(log n) time complexity.

  3. 3

    In the guessing game example with numbers 1 to 10,000, how many guesses does binary search take in the worst case?

    1. A

      10

    2. B

      10,000

    3. C

      100

    4. D

      14

    Correct answer
    D

    14

    Explanation

    The worst-case number of guesses is the ceiling of log⁔2(10000)ā‰ˆ13.3\log_2(10000) \approx 13.3log2​(10000)ā‰ˆ13.3, so 14 guesses.

  4. 4

    Depth-first search (DFS) visits nodes by:

    1. A

      Going as deep as possible along one branch before backtracking

    2. B

      Going level by level

    3. C

      Using a queue to process nodes

    4. D

      Randomly selecting unvisited nodes

    Correct answer
    A

    Going as deep as possible along one branch before backtracking

    Explanation

    DFS explores as far as possible along each branch before backtracking, which is the 'depth-first' approach.

  5. 5

    The time complexity of DFS is:

    1. A

      O(V + E)

    2. B

      O(E)

    3. C

      O(V log E)

    4. D

      O(V)

    Correct answer
    A

    O(V + E)

    Explanation

    DFS visits each vertex and edge once, leading to O(V+E) time complexity.

  6. 6

    In DFS, what is the purpose of the visited array?

    1. A

      To calculate the depth of each node

    2. B

      To keep track of backtracking points

    3. C

      To store the order of traversal

    4. D

      To avoid revisiting nodes that have already been visited

    Correct answer
    D

    To avoid revisiting nodes that have already been visited

    Explanation

    The visited array tracks which nodes have been visited to prevent infinite loops and redundant exploration.

  7. 7

    A real-life application of DFS mentioned in the video is:

    1. A

      Predicting stock prices

    2. B

      Sorting a list

    3. C

      Chess algorithms

    4. D

      Solving a maze

    Correct answer
    D

    Solving a maze

    Explanation

    The video mentions that DFS was originally created for solving mazes.

  8. 8

    Breadth-first search (BFS) processes nodes in what order?

    1. A

      Level by level (breadth-first)

    2. B

      Depth-first order

    3. C

      Reverse order

    4. D

      Random order

    Correct answer
    A

    Level by level (breadth-first)

    Explanation

    BFS visits all nodes at the current level before moving to the next level.

  9. 9

    Which data structure does BFS use to keep track of nodes to visit next?

    1. A

      Queue

    2. B

      Set

    3. C

      Priority queue

    4. D

      Stack

    Correct answer
    A

    Queue

    Explanation

    BFS uses a queue (neighbours queue) to process nodes level by level.

  10. 10

    The time complexity of BFS is:

    1. A

      O(V)

    2. B

      O(V log E)

    3. C

      O(V + E)

    4. D

      O(E)

    Correct answer
    C

    O(V + E)

    Explanation

    Like DFS, BFS also visits each vertex and edge once, giving O(V+E) time complexity.

  11. 11

    A real-life application of BFS mentioned in the video is:

    1. A

      Searching a phone book

    2. B

      Chess algorithms

    3. C

      Sorting a list

    4. D

      Solving a maze

    Correct answer
    B

    Chess algorithms

    Explanation

    The video mentions that BFS is used in chess algorithms to evaluate moves level by level.

  12. 12

    Which algorithm is best suited for finding an element in a sorted list?

    1. A

      Linear search

    2. B

      Binary search

    3. C

      Breadth-first search

    4. D

      Depth-first search

    Correct answer
    B

    Binary search

    Explanation

    Binary search is specifically designed for sorted lists and is efficient O(log n) compared to linear search O(n).

  13. 13

    In DFS, after reaching a dead end, the algorithm:

    1. A

      Stops

    2. B

      Restarts from the root

    3. C

      Backtracks to the most recent node with unvisited branches

    4. D

      Switches to BFS

    Correct answer
    C

    Backtracks to the most recent node with unvisited branches

    Explanation

    DFS backtracks to the previous node and continues exploring unvisited branches.

  14. 14

    Insertion sort works by:

    1. A

      Selecting a pivot and partitioning the list

    2. B

      Swapping adjacent elements repeatedly

    3. C

      Dividing the list into halves recursively

    4. D

      Comparing each element with the previous ones and inserting it in the correct position

    Correct answer
    D

    Comparing each element with the previous ones and inserting it in the correct position

    Explanation

    Insertion sort builds the sorted list one element at a time by inserting each new element into its proper place among the previously sorted elements.

  15. 15

    The best-case time complexity of insertion sort is:

    1. A

      O(n log n)

    2. B

      O(n)

    3. C

      O(n^2)

    4. D

      O(log n)

    Correct answer
    B

    O(n)

    Explanation

    When the list is already sorted, insertion sort only makes n-1 comparisons, so O(n).

  16. 16

    The worst-case time complexity of insertion sort is:

    1. A

      O(n log n)

    2. B

      O(n^2)

    3. C

      O(n)

    4. D

      O(log n)

    Correct answer
    B

    O(n^2)

    Explanation

    When the list is reverse sorted, insertion sort must compare and shift each element, leading to O(n^2) time.

  17. 17

    Insertion sort is most efficient when:

    1. A

      The list is stored in a linked list

    2. B

      The list contains many duplicate values

    3. C

      The list is large and unsorted

    4. D

      The list is small or nearly sorted

    Correct answer
    D

    The list is small or nearly sorted

    Explanation

    Insertion sort has O(n) best case on nearly sorted data and is simple for small lists.

  18. 18

    Merge sort is an example of which algorithmic paradigm?

    1. A

      Dynamic programming

    2. B

      Divide and conquer

    3. C

      Greedy

    4. D

      Backtracking

    Correct answer
    B

    Divide and conquer

    Explanation

    Merge sort splits the list into halves recursively (divide) and then merges sorted halves (conquer).

  19. 19

    Merge sort is a recursive algorithm. True or false?

    1. A

      True, it calls itself on subarrays

    2. B

      True, but only in the worst case

    3. C

      False, it uses iteration exclusively

    4. D

      False, it is an iterative algorithm

    Correct answer
    A

    True, it calls itself on subarrays

    Explanation

    Merge sort recursively divides the array into halves until subarrays of size 1, then merges them back up.

  20. 20

    The time complexity of merge sort in both best and worst cases is:

    1. A

      O(n)

    2. B

      O(n^2)

    3. C

      O(log n)

    4. D

      O(n log n)

    Correct answer
    D

    O(n log n)

    Explanation

    Merge sort always divides the list into halves (log n levels) and merges each level (n work), resulting in O(n log n).

  21. 21

    The space complexity of merge sort is:

    1. A

      O(log n)

    2. B

      O(1)

    3. C

      O(n log n)

    4. D

      O(n)

    Correct answer
    D

    O(n)

    Explanation

    Merge sort requires O(n) auxiliary space for merging temporary arrays.

  22. 22

    Quick sort selects a pivot and then:

    1. A

      Partitions the list into elements less than and greater than the pivot

    2. B

      Inserts the pivot at the correct position

    3. C

      Merges two sorted halves

    4. D

      Swaps adjacent elements until sorted

    Correct answer
    A

    Partitions the list into elements less than and greater than the pivot

    Explanation

    Quick sort partitions the list around a pivot so that elements less than pivot come before it and greater come after.

  23. 23

    The best-case time complexity of quick sort is:

    1. A

      O(log n)

    2. B

      O(n)

    3. C

      O(n^2)

    4. D

      O(n log n)

    Correct answer
    D

    O(n log n)

    Explanation

    When the pivot divides the list into two roughly equal halves, quick sort runs in O(n log n).

  24. 24

    The worst-case time complexity of quick sort is:

    1. A

      O(n^2)

    2. B

      O(n)

    3. C

      O(n log n)

    4. D

      O(log n)

    Correct answer
    A

    O(n^2)

    Explanation

    If the pivot is always the smallest or largest element, quick sort degrades to O(n^2).

  25. 25

    According to the video, which sorting algorithm is generally the fastest on average when implemented correctly?

    1. A

      Quick sort

    2. B

      Merge sort

    3. C

      They are all equally fast

    4. D

      Insertion sort

    Correct answer
    A

    Quick sort

    Explanation

    The video states that quick sort, when optimized, is 2-3 times faster than merge sort on average.

  26. 26

    The space complexity of quick sort (in-place) is:

    1. A

      O(1)

    2. B

      O(log n)

    3. C

      O(n)

    4. D

      O(n log n)

    Correct answer
    B

    O(log n)

    Explanation

    In-place quick sort uses O(log n) space for the recursion stack.

  27. 27

    One drawback of quick sort mentioned in the video is:

    1. A

      It is harder to implement correctly and any inefficiency can cause it to run slower

    2. B

      It cannot be used on large lists

    3. C

      It requires a lot of extra memory

    4. D

      It is slower than merge sort on average

    Correct answer
    A

    It is harder to implement correctly and any inefficiency can cause it to run slower

    Explanation

    The video notes that quick sort is harder to implement correctly; any mistake can degrade performance.

  28. 28

    A greedy algorithm makes decisions based on:

    1. A

      Random selection

    2. B

      The worst possible choice

    3. C

      The best local choice at each step

    4. D

      The overall optimal solution

    Correct answer
    C

    The best local choice at each step

    Explanation

    Greedy algorithms choose the best option at each decision point without considering future consequences.

  29. 29

    Why might a greedy algorithm not find the optimal solution?

    1. A

      It only looks at the immediate best option, which may lead to a suboptimal path

    2. B

      It is too slow

    3. C

      It requires exponential time

    4. D

      It considers all future possibilities

    Correct answer
    A

    It only looks at the immediate best option, which may lead to a suboptimal path

    Explanation

    The local optimal choice may not lead to the global optimum, as shown in the example with path costs.

  30. 30

    When is it acceptable to use a greedy algorithm?

    1. A

      When the input size is small

    2. B

      When the optimal solution is easy to compute

    3. C

      When the problem is too large to compute the exact optimal solution

    4. D

      When you need the fastest algorithm possible

    Correct answer
    C

    When the problem is too large to compute the exact optimal solution

    Explanation

    Greedy algorithms are used when the exact optimal solution is computationally infeasible, to get a reasonably good answer.

  31. 31

    In the traveling salesman problem with n cities, the number of possible routes is:

    1. A

      n!

    2. B

      2^n

    3. C

      n^2

    4. D

      (n-1)!/2

    Correct answer
    D

    (n-1)!/2

    Explanation

    The video gives the formula (nāˆ’1)!/2(n-1)!/2(nāˆ’1)!/2 for total possible routes in the traveling salesman problem.

  32. 32

    In the path cost example from the video, the optimal cost was $3, but the greedy algorithm achieved a cost of:

    1. A

      9

    2. B

      16

    3. C

      7

    4. D

      3

    Correct answer
    B

    16

    Explanation

    The greedy algorithm chose 7 and then 9 totaling 16, while the optimal path cost only 3.

  33. 33

    Which of the following is a characteristic of greedy algorithms?

    1. A

      They are the most efficient sorting algorithms

    2. B

      They make locally optimal choices

    3. C

      They always produce the optimal solution

    4. D

      They require backtracking

    Correct answer
    B

    They make locally optimal choices

    Explanation

    Greedy algorithms are defined by making the best local decision at each step.

Question 1 of 33
Question 1 of 33

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent