# S&P Global DSA Questions with Answers

6 previously-asked dsa questions from S&P Global'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. Remove duplicates from a sorted array in place and return the new length.

**Answer:** Use a slow write pointer starting at index 0. Iterate with a fast pointer; when the current element differs from the last written one, write it at the next slow position. The slow index plus 1 is the new length. Time O(n), space O(1).

**Explanation:** Two-pointer overwrite on a sorted array.

### 2. Merge a list of overlapping intervals.

**Answer:** Sort intervals by start. Iterate, and if the current interval overlaps the last merged one (start is at most the last end), extend the last end; otherwise append it. Time O(n log n).

**Explanation:** Sort by start, then merge overlaps.

### 3. Find the k most frequent elements in an array.

**Answer:** Build a frequency map, then use a heap of size k or bucket sort by frequency to pick the top k. Time O(n log k) with a heap, or O(n) with bucket sort.

**Explanation:** Frequency map then heap or bucket sort.

### 4. Compute the moving average over a sliding window of size k in a stream.

**Answer:** Maintain a queue of the last k values and a running sum. On each new value, add it and enqueue; if the size exceeds k, dequeue and subtract. The average is sum divided by the current count. O(1) per update.

**Explanation:** Queue plus a running sum.

### 5. Search for a target value in a sorted array.

**Answer:** Use binary search: maintain low and high, compute mid, and narrow to the half that may contain the target until found or the range is empty. Time O(log n).

**Explanation:** Binary search on the sorted array.

### 6. Given daily stock prices, find the maximum profit from one buy and one later sell.

**Answer:** Track the minimum price seen so far and the best profit. For each price, update the minimum, then update the best profit as price minus minimum. Time O(n), space O(1).

**Explanation:** Track running minimum and best profit.

More S&P Global preparation, including the full recruitment process: https://useastra.in/campus/s-and-p-global
