# IBM DSA Questions with Answers

6 previously-asked dsa questions from IBM'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. Count the number of vowels and consonants in a string.

**Answer:** Iterate the string; for each alphabetic character, increment the vowel count if it is a, e, i, o, or u, otherwise the consonant count. Ignore non-letters. Time O(n).

**Explanation:** Classify each alphabetic character.

### 2. Find the largest and second largest elements in a single pass.

**Answer:** Track largest and second, both set very low. For each element, if it exceeds largest, move largest to second and update largest; else if it exceeds second and differs from largest, update second. Time O(n).

**Explanation:** Single pass tracking top two distinct values.

### 3. Check whether a sentence is a pangram (contains every letter of the alphabet).

**Answer:** Add each alphabetic character in lower case to a set. After scanning, the string is a pangram if the set contains all 26 letters. Time O(n).

**Explanation:** Collect distinct letters, check for 26.

### 4. Find the elements common to three sorted arrays.

**Answer:** Use three pointers. If all point to equal values, record it and advance all; otherwise advance the pointer at the smallest value. Time O(n1 + n2 + n3).

**Explanation:** Three-pointer merge across sorted arrays.

### 5. Implement basic run-length string compression (for example aaabb becomes a3b2).

**Answer:** Scan the string counting consecutive equal characters; append the character and its count to the result when the run ends. Return the original if compression is not shorter. Time O(n).

**Explanation:** Count consecutive runs and emit char plus count.

### 6. Find the majority element (appearing more than n/2 times) using a hash map.

**Answer:** Count occurrences in a hash map in one pass, then return the element whose count exceeds n/2. Time O(n), space O(n). Boyer-Moore voting does it in O(1) space.

**Explanation:** Frequency map, return the element over n/2.

More IBM preparation, including the full recruitment process: https://useastra.in/campus/ibm
