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

  • 27Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Search Algorithms
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is binary search and what condition must the list satisfy for it to work?

Tap to flipClick to flipspace

Binary search is a search algorithm that finds the position of a target element in a sorted list. It works only on sorted lists.

Or swipe the card

27 cards

  1. What is binary search and what condition must the list satisfy for it to work?

    Binary search is a search algorithm that finds the position of a target element in a sorted list. It works only on sorted lists.

  2. What is the time complexity of binary search?

    O(log⁡n)O(\log n)O(logn) (base 2).

  3. What is linear search and its time complexity?

    Linear search checks each element sequentially. Its time complexity is O(n)O(n)O(n).

  4. How does depth-first search (DFS) traverse a graph?

    DFS starts at the root and explores as far as possible along each branch before backtracking.

  5. What data structure does DFS commonly use (implicitly)?

    A stack (or recursion stack).

  6. What is the time complexity of DFS (and BFS) on a graph?

    O(V+E)O(V + E)O(V+E), where VVV is vertices and EEE is edges.

  7. Give a real-life example where DFS is useful.

    Solving a maze.

  8. How does breadth-first search (BFS) traverse a graph?

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

  9. What data structure does BFS use?

    A queue.

  10. Give a real-life example where BFS is used.

    Chess algorithms (evaluating all possible moves at each turn).

  11. What is the key difference between DFS and BFS traversal order?

    DFS goes deep (branch by branch); BFS goes wide (level by level).

  12. Describe insertion sort's process.

    Insertion sort builds a sorted list by repeatedly taking the next element and inserting it into the correct position among the previously sorted elements.

  13. What are the best-case and worst-case time complexities of insertion sort?

    Best-case: O(n)O(n)O(n); worst-case: O(n2)O(n^2)O(n2).

  14. When is insertion sort a good choice?

    For small lists or lists that are mostly sorted.

  15. Describe merge sort's process.

    Merge sort divides the list into halves recursively until each sublist has one element, then merges pairs back in sorted order.

  16. What is the time complexity of merge sort in all cases?

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

  17. When should merge sort be used over insertion sort?

    For larger, unsorted lists because it guarantees O(nlog⁡n)O(n \log n)O(nlogn) time.

  18. Describe quick sort's process.

    Quick sort picks a pivot, partitions the list into elements less than and greater than the pivot, then recursively sorts the sublists.

  19. What are the best-case and worst-case time complexities of quick sort?

    Best-case: O(nlog⁡n)O(n \log n)O(nlogn); worst-case: O(n2)O(n^2)O(n2).

  20. Why is quick sort often faster than merge sort in practice?

    Its inner loop can be optimized, and on average it is 2-3 times faster than merge sort.

  21. How does the space complexity of quick sort compare to merge sort?

    Quick sort uses O(log⁡n)O(\log n)O(logn) space (in-place), while merge sort uses O(n)O(n)O(n) space.

  22. What is a major drawback of quick sort?

    It is sensitive to implementation; mistakes can make it much slower, and it is harder to implement correctly.

  23. Define a greedy algorithm.

    A greedy algorithm makes the best local choice at each step, hoping to find a global optimum.

  24. When do greedy algorithms fail to find the optimal solution?

    When the best local choice does not lead to the global optimum, as in the path cost example (choosing 7 then 9 yields cost 16, but optimal path costs 3).

  25. When are greedy algorithms useful despite not guaranteeing optimality?

    When the problem is too large to compute an exact solution (e.g., NP-hard problems like the Travelling Salesman Problem).

  26. What is the Travelling Salesman Problem (TSP)?

    TSP asks for the shortest possible route that visits each city once and returns to the origin city.

  27. How can a greedy algorithm approach the TSP?

    Start at an arbitrary city and always choose the nearest unvisited city as the next destination.

Card 1 of 27
Card 1 of 27

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent