# HCLTech DSA Questions with Answers

6 previously-asked dsa questions from HCLTech'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. Check whether a number is a perfect number.

**Answer:** A perfect number equals the sum of its proper divisors (for example 6 = 1 + 2 + 3). Sum all divisors from 1 to n/2 and compare with n. Time O(n) or O(square root of n) with paired divisors.

**Explanation:** Sum of proper divisors equals the number.

### 2. Find the longest word in a sentence.

**Answer:** Split the sentence into words, track the word with the maximum length while iterating, and return it. Handle ties by keeping the first. Time O(n).

**Explanation:** Scan words tracking the maximum length.

### 3. Implement linear search on an array.

**Answer:** Iterate from the first element to the last, comparing each with the target. Return the index on a match, or -1 if the loop finishes without finding it. Time O(n).

**Explanation:** Sequential scan for the target.

### 4. Count the number of leaf nodes in a binary tree.

**Answer:** Traverse the tree (recursively or with a stack). A node is a leaf when both its children are null; increment a counter for each such node. Time O(n).

**Explanation:** Count nodes with no children.

### 5. Find the sum of all even numbers in an array.

**Answer:** Iterate the array, and for each element that is divisible by 2 add it to a running total. Return the total. Time O(n).

**Explanation:** Add elements where value mod 2 is 0.

### 6. Convert a binary number to its decimal equivalent.

**Answer:** Process the binary digits from left to right, doing result = result * 2 + digit for each bit. Alternatively sum each bit times its power of two. Time O(number of bits).

**Explanation:** Horner method: result * 2 + bit.

More HCLTech preparation, including the full recruitment process: https://useastra.in/campus/hcl
