# Mphasis DSA Questions with Answers

6 previously-asked dsa questions from Mphasis'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. Find the length of a string without using a built-in library function.

**Answer:** Initialise a counter to 0 and iterate from the first character until the null terminator (or end), incrementing the counter for each character. Return the counter. Time O(n).

**Explanation:** Count characters until the terminator.

### 2. Write logic to check whether a given year is a leap year.

**Answer:** A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. Return true or false accordingly.

**Explanation:** Divisible by 4 and not 100, or by 400.

### 3. Print the reverse of an integer.

**Answer:** Initialise result to 0. Repeatedly take the last digit with n mod 10, do result = result * 10 + digit, then n = n / 10 until n is 0. Handle sign and possible overflow.

**Explanation:** Build the reversed number digit by digit.

### 4. Find both the largest and smallest elements in an array in one pass.

**Answer:** Initialise min and max to the first element. For each remaining element, update min if smaller and max if larger. Return both after the single scan. Time O(n).

**Explanation:** Track min and max in one scan.

### 5. Check whether a given number is a palindrome.

**Answer:** Reverse the number using the modulo and divide technique, then compare the reversed value with the original. If equal it is a palindrome. Time O(number of digits).

**Explanation:** Reverse and compare with the original.

### 6. Count the number of set bits (1s) in the binary representation of an integer.

**Answer:** Repeatedly do count = count + (n AND 1), then right shift n by 1, until n is 0. Brian Kernighan method n = n AND (n-1) counts only set bits and is faster.

**Explanation:** Bit masking or Brian Kernighan trick.

More Mphasis preparation, including the full recruitment process: https://useastra.in/campus/mphasis
