← Microsoft Interview Insights

Microsoft·Data Scientist·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Three pretty dense coding problems for a Data Scientist role at Microsoft. The questions leaned heavily algorithmic, which I wasn't fully expecting. Left feeling okay about two of them and genuinely shaky on the third.

Questions Asked (3)

Q1

Given a sorted integer array and a value k, modify the array in-place so each distinct element appears at most k times. Return the new valid length. Must run in O(n) time with O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Two-pointer setup is the right instinct but I kept second-guessing my write index.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer (write) tracks the position to place the next valid element, and another (read) scans the array. Since the array is sorted, you can allow up to k duplicates by comparing the current element with the element at write - k. This achieves O(n) time and O(1) space.

Pro tip: Emphasize that the array is sorted, which allows in-place deduplication without extra space. Also, mention that the solution is optimal and discuss edge cases like k=0 or k >= array length.

1. Clarify requirements and edge cases

Confirm that the array is sorted, k is a non-negative integer, and modifications must be in-place. Discuss edge cases: empty array, k=0, k >= length, and all elements identical.

2. Initialize write pointer

Set write = 0 to track the position where the next valid element will be placed. Iterate through the array with a read pointer.

3. Iterate and conditionally write

For each element at read, if write < k or the current element is different from the element at write - k, then assign array[write] = array[read] and increment write.

4. Return new length

After the loop, write represents the new length of the modified array. Return write.

5. Analyze complexity and trade-offs

Explain that the algorithm runs in O(n) time with O(1) extra space. Discuss why this is optimal and mention potential variations (e.g., if array were not sorted).

Key Points to Mention

  • Two-pointer technique for in-place modification
  • Leveraging sorted order to detect duplicates
  • Condition: allow up to k duplicates by comparing with element at write - k
  • Time complexity O(n) and space complexity O(1)
  • Edge cases: k=0, k >= n, empty array
  • In-place modification without using extra data structures

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Find the shortest substring of s that contains all characters of t (with correct frequencies). If there's a tie, return the leftmost. Must run in O(|s| + |t|) time and handle Unicode code points correctly.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Minimum window substring.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with two pointers to expand and contract the window while maintaining character counts of t. Track the minimum window length and leftmost start index, ensuring O(|s| + |t|) time by processing each character at most twice. Handle Unicode by iterating over code points (e.g., using runes in Go or code points in Python) rather than bytes.

Pro tip: Explicitly state that you will treat the string as a sequence of Unicode code points, not bytes, and mention that in languages like Python 3, iterating over a string already yields code points, but in others you may need to decode first. This shows attention to detail and avoids a common pitfall.

1. Clarify requirements and edge cases

Confirm that the substring must contain all characters of t with correct frequencies, and if multiple shortest substrings exist, return the leftmost. Discuss edge cases: empty t, t longer than s, or characters not in s.

2. Choose data structures

Use a hash map (or fixed-size array if the character set is small) to store the frequency of each character in t. Maintain a count of how many characters from t are currently satisfied in the window.

3. Implement sliding window

Initialize left and right pointers at 0. Expand right to include characters, updating the window frequency and satisfied count. When all characters are satisfied, shrink left to find the smallest valid window, updating the minimum length and start index.

4. Handle Unicode code points

Ensure that iteration and frequency counting operate on Unicode code points (e.g., using runes in Go, code points in Python, or codePointAt in Java) to correctly handle characters outside the Basic Multilingual Plane.

5. Analyze complexity and test

Explain that each character is added and removed at most once, giving O(|s| + |t|) time and O(|t|) space. Walk through a small example and test edge cases.

Key Points to Mention

  • Sliding window technique with two pointers
  • Frequency counting using hash map or array
  • Maintaining a count of satisfied characters to avoid rescanning
  • Time complexity O(|s| + |t|) and space O(|t|)
  • Handling Unicode code points correctly (not bytes)
  • Tie-breaking: return leftmost substring when lengths are equal

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Implement a sanitize function that trims whitespace, collapses internal whitespace runs into a single space, and replaces every maximal run of ASCII digits with a single '#' token. Do it in a single O(n) pass with O(1) extra space. Discuss multi-byte Unicode and surrogate pair pitfalls.

Algorithms & Data StructuresSystem Design
Author's notes

This one felt less like a classic LC problem and more like something you'd actually write at work, which I liked.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and edge cases, then outline a two-pointer in-place algorithm that processes the string character by character, handling whitespace and digit runs. Emphasize the O(n) time and O(1) space constraints, and discuss Unicode pitfalls such as multi-byte characters and surrogate pairs.

Pro tip: Mention that the function should operate on a mutable character array (e.g., in C++ or Java) to achieve O(1) space, and that for immutable strings (like in Python), you may need to convert to a list first, which uses O(n) space—so clarify the language context.

1. Clarify requirements and constraints

Ask about input type (string vs. char array), whether the function should modify in-place, and how to handle Unicode. Confirm that '#' replaces digit runs and that whitespace includes spaces, tabs, newlines.

2. Design the two-pointer algorithm

Use a read pointer to scan the input and a write pointer to build the output. Maintain a state flag for whether the previous character was whitespace or a digit to collapse runs.

3. Handle whitespace and digit runs

When encountering whitespace, write a single space only if the last written character wasn't a space. When encountering a digit, write '#' only if the previous character wasn't a digit.

4. Address Unicode and surrogate pairs

Explain that ASCII digits are safe, but for Unicode, code points may be multi-byte. Surrogate pairs in UTF-16 must be treated as a single character; naive byte-wise processing can split them.

5. Analyze complexity and edge cases

Confirm O(n) time and O(1) extra space (if mutable array). Test edge cases: empty string, all whitespace, all digits, leading/trailing whitespace, and mixed Unicode.

Key Points to Mention

  • Two-pointer technique for in-place modification
  • State tracking to collapse runs (whitespace and digits)
  • O(n) time and O(1) extra space (with mutable array)
  • Unicode: multi-byte characters and surrogate pairs
  • ASCII digit definition (0-9) vs. Unicode digits
  • Edge cases: empty string, all whitespace, all digits, leading/trailing whitespace

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.