# Hexaware DSA Questions with Answers

6 previously-asked dsa questions from Hexaware'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. Given an array and a target, return the indices of the two numbers that add up to the target.

**Answer:** Use a hash map from value to index. For each element x, check if target minus x is already in the map; if so return the two indices, otherwise store x with its index. Time O(n).

**Explanation:** Hash map complement lookup (Two Sum).

### 2. Reverse the digits of an integer, returning 0 on overflow.

**Answer:** Pop digits with modulo and divide, pushing them into a result while checking for overflow before each multiply and add. Preserve the sign. Time O(number of digits).

**Explanation:** Digit-by-digit reversal with overflow check.

### 3. Check whether a string of brackets is balanced.

**Answer:** Push every opening bracket onto a stack. For each closing bracket, the stack top must be the matching opening bracket, otherwise it is unbalanced. At the end the stack must be empty. Time O(n).

**Explanation:** Stack matching of brackets.

### 4. Compute the factorial of a number using recursion.

**Answer:** Define factorial(n) = 1 for n = 0 or 1, otherwise n * factorial(n-1). Ensure a correct base case to stop recursion and use a 64-bit type to reduce overflow.

**Explanation:** Recursive definition with a base case.

### 5. Find the nth Fibonacci number efficiently.

**Answer:** Use iteration with two variables updated n times for O(n) time and O(1) space, or memoisation to avoid recomputation. Avoid naive recursion which is exponential.

**Explanation:** Iterative or memoised, not naive recursion.

### 6. Move all zeros in an array to the end while keeping the order of non-zero elements.

**Answer:** Keep a write index. Iterate the array, and whenever you see a non-zero element write it at the write index and advance it. After the pass, fill the remaining positions with zeros. Time O(n), space O(1).

**Explanation:** Two-pointer stable partition.

More Hexaware preparation, including the full recruitment process: https://useastra.in/campus/hexaware
