Solve sliding window, heap, DP, in-place tasks
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This set evaluates proficiency with core algorithmic patterns—sliding-window techniques, heap-based top-k selection, dynamic programming for unbounded combinations, frequency-count windowing for anagrams, and in-place array permutation manipulation—alongside complexity analysis and robust edge-case handling.
Longest Substring with All Distinct Characters
Constraints
- 0 <= len(s)
- s contains ASCII / lowercase letters
- Target O(n) time, single pass
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: "abc" is the longest window without repeats.
Input: ("bbbbb",)
Expected Output: 1
Explanation: All characters identical; best window is a single 'b'.
Input: ("pwwkew",)
Expected Output: 3
Explanation: "wke" has length 3; "pwke" would repeat 'w'.
Input: ("",)
Expected Output: 0
Explanation: Empty string has no substring.
Input: ("abcdef",)
Expected Output: 6
Explanation: All distinct, the whole string qualifies.
Input: ("au",)
Expected Output: 2
Explanation: Both distinct.
Input: ("dvdf",)
Expected Output: 3
Explanation: Window must jump only past the previous 'd', giving "vdf".
Hints
- Maintain a window [start, i]; remember the last index where each character appeared.
- When a character repeats inside the window, jump start to last[ch] + 1 instead of resetting to 0.
- Update the answer with the current window length at every step.
K Closest Points to the Origin
Constraints
- 1 <= k <= len(points)
- Each point is [x, y] with integer coordinates
- Distance is the squared Euclidean distance x*x + y*y (no sqrt needed)
- Any ordering of the k closest points is valid
Examples
Input: (1, [[1, 3], [-2, 2]])
Expected Output: [[-2, 2]]
Explanation: Distances 10 vs 8; [-2,2] is closer.
Input: (2, [[3, 3], [5, -1], [-2, 4]])
Expected Output: [[3, 3], [-2, 4]]
Explanation: Distances 18, 26, 20; the two smallest are 18 and 20.
Input: (1, [[0, 0]])
Expected Output: [[0, 0]]
Explanation: Single point at the origin.
Input: (3, [[1, 0], [0, 1], [2, 0], [0, 2]])
Expected Output: [[1, 0], [0, 1], [2, 0]]
Explanation: Distances 1,1,4,4; pick the three smallest.
Input: (2, [[-1, -1], [-2, -2], [3, 3]])
Expected Output: [[-1, -1], [-2, -2]]
Explanation: Distances 2, 8, 18; the two closest.
Input: (2, [[5, 5], [5, 5]])
Expected Output: [[5, 5], [5, 5]]
Explanation: Duplicate points with equal distance; both returned.
Hints
- Comparing squared distances avoids floating point and preserves ordering.
- Keep a max-heap of size k (store negated distance) so the farthest of the current best k is at the top.
- If a new point is closer than the heap's max, replace the top; this keeps the heap at size k for O(n log k).
Minimum Items to Reach a Target Sum
Constraints
- All values are positive integers
- Each value can be used unlimited times (unbounded)
- 0 <= amount
- Return -1 when amount is unreachable
Examples
Input: ([1, 2, 5], 11)
Expected Output: 3
Explanation: 5 + 5 + 1 uses 3 items.
Input: ([2], 3)
Expected Output: -1
Explanation: Only even sums are reachable with 2; 3 is impossible.
Input: ([1], 0)
Expected Output: 0
Explanation: Amount 0 needs zero items.
Input: ([2, 5, 10, 1], 27)
Expected Output: 4
Explanation: 10 + 10 + 5 + 2 = 27 uses 4 items.
Input: ([3, 7], 14)
Expected Output: 2
Explanation: 7 + 7 = 14.
Input: ([5], 5)
Expected Output: 1
Explanation: A single 5.
Input: ([7], 3)
Expected Output: -1
Explanation: 3 < 7, unreachable.
Hints
- Define dp[a] = minimum number of values summing exactly to a.
- Base case dp[0] = 0; initialize the rest to a sentinel larger than any valid answer (amount + 1).
- For each a, try every value v <= a: dp[a] = min(dp[a], dp[a - v] + 1). If dp[amount] stays at the sentinel, return -1.
Find All Anagram Start Indices
Constraints
- s and p consist of lowercase English letters
- Return an empty list if len(p) > len(s)
- Indices are returned in ascending order
- Target O(|s|) using fixed-size window counts
Examples
Input: ("cbaebabacd", "abc")
Expected Output: [0, 6]
Explanation: "cba" starts at 0 and "bac" starts at 6.
Input: ("abab", "ab")
Expected Output: [0, 1, 2]
Explanation: "ab", "ba", "ab" are all anagrams of "ab".
Input: ("af", "be")
Expected Output: []
Explanation: No window matches the letters of "be".
Input: ("", "a")
Expected Output: []
Explanation: p longer than s, no windows.
Input: ("a", "a")
Expected Output: [0]
Explanation: Single matching window at index 0.
Input: ("aaaa", "aa")
Expected Output: [0, 1, 2]
Explanation: Every length-2 window is "aa".
Input: ("abc", "abcd")
Expected Output: []
Explanation: p longer than s.
Hints
- Build a frequency array for p; maintain a frequency array for the current window of length len(p).
- When the window index passes len(p), decrement the count of the character that just left the window.
- Whenever the window count array equals p's count array, record the window's start index i - len(p) + 1.
Previous Permutation With One Swap
Constraints
- Modify in place; O(n) or O(n log n) time
- At most one swap of two indices is allowed
- Return a unchanged when it is already the smallest such permutation
- Handle duplicates and repeated values near the pivot
Examples
Input: ([3, 2, 1],)
Expected Output: [3, 1, 2]
Explanation: Pivot at index 1 (2>1); swap 2 and 1 -> [3,1,2].
Input: ([1, 1, 5],)
Expected Output: [1, 1, 5]
Explanation: Non-decreasing, no descent; already smallest, return unchanged.
Input: ([1, 9, 4, 6, 7],)
Expected Output: [1, 7, 4, 6, 9]
Explanation: Pivot 9 at index 1; largest value < 9 to the right is 7; swap them.
Input: ([3, 1, 1, 3],)
Expected Output: [1, 3, 1, 3]
Explanation: Pivot 3 at index 0; swap with the leftmost 1 to keep the result largest.
Input: ([1, 2, 3],)
Expected Output: [1, 2, 3]
Explanation: Strictly increasing, already the smallest permutation, no swap reduces it.
Input: ([5],)
Expected Output: [5]
Explanation: Single element, nothing to swap.
Input: ([2, 2, 2],)
Expected Output: [2, 2, 2]
Explanation: All equal; no swap produces a strictly smaller array.
Hints
- Find the rightmost index i where a[i] > a[i+1]; left of and at i nothing can be made smaller by a single swap.
- Swap a[i] with the largest value to its right that is strictly less than a[i] to maximize the result while still decreasing.
- If that largest smaller value appears more than once, swap with its leftmost occurrence so the larger duplicates stay further right (keeping the array lexicographically larger).