Algorithms questions and answers

Complexity, sorting, searching, recursion and DP. Practise algorithms questions from the technical & programming section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample algorithms questions with worked answers

Question 1

What is the worst-case time complexity of binary search on a sorted array of n elements?

  1. O(n)
  2. O(log n)
  3. O(n log n)
  4. O(1)

Answer: B. O(log n)

Explanation: Each comparison halves the remaining search range, so at most about log2(n) + 1 comparisons are needed.

Question 2

What is the solution of the recurrence T(n) = 2T(n/2) + n, with T(1) = 1?

  1. O(n)
  2. O(n²)
  3. O(log n)
  4. O(n log n)

Answer: D. O(n log n)

Explanation: There are log2 n levels of recursion and each level does a total of n work, giving O(n log n). This is the recurrence of merge sort.

Question 3

What is the length of the Longest Common Subsequence (LCS) of the strings "ABCBDAB" and "BDCABA"?

  1. 3
  2. 4
  3. 5
  4. 6

Answer: B. 4

Explanation: Filling the standard LCS DP table gives 4; one such subsequence is "BCBA". No common subsequence of length 5 exists.

All 30 algorithms questions with answers

Algorithms: common questions

What are algorithms questions?

Complexity, sorting, searching, recursion and DP. They appear in the technical & programming section of many placement tests.

Where can I practise algorithms questions with answers for free?

Right here on LogicWiz Exams. Every algorithms question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise algorithms?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of algorithms questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Technical & Programming practice.

More technical & programming topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep