# Infosys DSA Questions with Answers

6 previously-asked dsa questions from Infosys's hiring process, each with the correct answer and a worked explanation. Written against the company's actual test pattern.

_Source: Astra (https://useastra.in). Updated 2026-09-05._

### 1. Write an approach to find the largest of three numbers.

**Answer:** Compare the first with the second to get the larger, then compare that result with the third. Alternatively use nested if or the max function. Return the greatest value. Time O(1).

**Explanation:** Pairwise comparison of three values.

### 2. How do you check whether a number is prime?

**Answer:** A number less than 2 is not prime. Otherwise test divisors from 2 up to the square root of n; if any divides n evenly it is not prime, else it is prime. Time O(square root of n).

**Explanation:** Trial division up to the square root.

### 3. Print the Fibonacci series up to n terms.

**Answer:** Start with a = 0 and b = 1. Print a, then repeatedly compute next = a + b, shift a = b and b = next, for n terms. Iterative solution is O(n) time and O(1) space.

**Explanation:** Iterative two-variable approach.

### 4. Count the number of vowels in a string.

**Answer:** Loop through each character, convert to lowercase, and check if it is one of a, e, i, o, u; increment a counter when it matches. Time O(n).

**Explanation:** Single pass with a vowel check.

### 5. Swap two numbers without using a temporary variable.

**Answer:** Use arithmetic: a = a + b; b = a - b; a = a - b. Or use XOR: a = a ^ b; b = a ^ b; a = a ^ b. Both swap the values in place.

**Explanation:** Arithmetic or XOR swap.

### 6. Explain how bubble sort works.

**Answer:** Repeatedly step through the list comparing adjacent pairs and swapping them if out of order, so the largest element bubbles to the end each pass. Repeat for n-1 passes. Time O(n^2), space O(1). It is stable.

**Explanation:** Adjacent compare and swap, repeated per pass.

More Infosys preparation, including the full recruitment process: https://useastra.in/campus/infosys
