🚀 Coding Interview Questions with Answers (Part 9)
9️⃣1️⃣ What is an Algorithm?
Answer:
An algorithm is a finite, step-by-step set of instructions designed to solve a specific problem or perform a task efficiently.
Characteristics of a Good Algorithm:
• Well-defined inputs and outputs
• Unambiguous steps
• Finite number of steps
• Efficient in terms of time and memory
• Produces the correct result
Example: Sorting a list of numbers or finding the shortest path in a graph.
9️⃣2️⃣ What is Time Complexity?
Answer:
Time complexity measures the amount of time an algorithm takes to execute as the input size ("n") increases.
It helps compare the efficiency of different algorithms without depending on hardware or programming language.
Common Time Complexities:
• O(1): Constant time
• O(log n): Logarithmic time
• O(n): Linear time
• O(n log n): Linearithmic time
• O(n²): Quadratic time
• O(2ⁿ): Exponential time
• O(n!): Factorial time
9️⃣3️⃣ What is Space Complexity?
Answer:
Space complexity measures the amount of memory an algorithm requires during execution relative to the input size.
It includes:
• Input storage
• Auxiliary (temporary) memory
• Recursive call stack
Efficient algorithms aim to optimize both time and space complexity.
9️⃣4️⃣ What is Big O Notation?
Answer:
Big O notation describes the upper bound (worst-case) time or space complexity of an algorithm.
It shows how the algorithm's performance grows as the input size increases.
Examples:
• Accessing an array element → O(1)
• Linear Search → O(n)
• Binary Search → O(log n)
• Merge Sort → O(n log n)
9️⃣5️⃣ What is Big Theta (Θ) Notation?
Answer:
Big Theta (Θ) notation describes the exact or tight bound of an algorithm's complexity.
It indicates that the algorithm performs within both the upper and lower bounds for large input sizes.
Example:
Merge Sort has a time complexity of Θ(n log n) because it consistently performs at that rate in the best, average, and worst cases.
9️⃣6️⃣ What is Big Omega (Ω) Notation?
Answer:
Big Omega (Ω) notation describes the lower bound (best-case) time complexity of an algorithm.
It represents the minimum amount of time an algorithm will take under the best possible conditions.
Example:
Linear Search has a best-case complexity of Ω(1) when the target element is found at the first position.
9️⃣7️⃣ What is Binary Search?
Answer:
Binary Search is a searching algorithm that finds an element in a sorted array by repeatedly dividing the search range in half.
Steps:
1. Find the middle element.
2. Compare it with the target.
3. Search the left or right half accordingly.
4. Repeat until the element is found or the search space becomes empty.
Time Complexity: O(log n)
Requirement: The array must be sorted.
9️⃣8️⃣ What is Linear Search?
Answer:
Linear Search checks each element one by one until the target element is found or the end of the collection is reached.
Advantages:
• Works on both sorted and unsorted data.
• Easy to implement.
Time Complexity: O(n)
9️⃣9️⃣ What is the Difference Between Linear Search and Binary Search?
Answer:
Linear Search