← HubSpot Interview Insights

HubSpot·Software Engineer·Technical Phone Screen·Junior

Junior
Apr 2026

Summary

Got a coding screen at HubSpot that was pretty much a straightforward array manipulation problem. Nothing crazy, but the in-place constraint is where people slip up if they're not careful.

Questions Asked (1)

Q1

Given an integer array and a target value, remove all occurrences of that value in place and return the count of remaining elements. The first k elements of the modified array should hold the valid values in any order. Must run in O(n) time with O(1) extra space.

Algorithms & Data Structures
Author's notes

Two pointers is the move here and I did get there, but I fumbled around for a bit first thinking about a second array before remembering the space constraint.

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 the other (read) scans through the array. For each element, if it's not the target, write it at the write index and increment write. Finally, return write as the count of remaining elements.

Pro tip: Clarify that the order of the remaining elements doesn't matter, so you can optimize by swapping with the last element when you encounter the target, reducing the number of writes. However, the two-pointer approach is simpler and still O(n).

1. Understand the problem and constraints

Restate the problem: remove all instances of a given value in-place, return the new length, and ensure the first k elements contain the valid values. Emphasize O(n) time and O(1) space.

2. Choose the two-pointer technique

Explain that you'll use two pointers: one for reading through the array and one for writing the next valid element. This avoids extra space and processes each element once.

3. Walk through the algorithm

Initialize write pointer to 0. Iterate read pointer from 0 to n-1. If nums[read] != target, set nums[write] = nums[read] and increment write. After the loop, return write.

4. Analyze complexity and edge cases

State that time complexity is O(n) because each element is visited once, and space is O(1) since only two pointers are used. Mention edge cases: empty array, all elements equal to target, no elements equal to target.

5. Test with an example

Walk through a small example, such as nums = [3,2,2,3], target = 3, showing how the array transforms and the return value is 2.

Key Points to Mention

  • In-place modification means we cannot use extra arrays; we must rearrange the given array.
  • The two-pointer approach ensures O(n) time and O(1) space.
  • The order of the remaining elements does not matter, so we can simply overwrite.
  • Return the count of valid elements (k), not the modified array.
  • Edge cases: empty array, all elements equal to target, no elements equal to target.
  • The algorithm is stable in the sense that it preserves the relative order of the remaining elements, though not required.

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