PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Solve sliding window, heap, DP, in-place tasks

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given several LeetCode-style coding tasks. Implement each with the stated time/space goals and handle edge cases. ## 1) Sliding Window: longest substring with all distinct characters **Input:** a string `s` (ASCII or lowercase letters). **Output:** an integer: the maximum length of a contiguous substring of `s` that contains **no repeated characters**. **Constraints:** target time `O(n)`. --- ## 2) Heap / Top-K: k closest points **Input:** an integer `k` and an array of points `points`, where each point is `(x, y)`. **Output:** any ordering of the **k points** with smallest squared distance to the origin, `x^2 + y^2`. **Constraints:** must be `O(n log k)` time (and `O(k)` extra space besides output). --- ## 3) DP: minimum items to reach a target sum **Input:** an array of positive integers `values` (can reuse each value unlimited times) and an integer `amount`. **Output:** the minimum number of values needed to sum to `amount`; return `-1` if impossible. **Constraints:** explain state definition and initialization clearly; typical target `O(len(values) * amount)` time. --- ## 4) Sliding Window: find all anagram start indices **Input:** strings `s` and `p`. **Output:** all starting indices `i` such that `s[i : i+len(p)]` is a permutation of `p`. **Constraints:** target `O(|s|)` time using a fixed-size window and character counts. --- ## 5) Array in-place: previous permutation with one swap **Input:** an integer array `a`. **Output:** the lexicographically largest array that is **strictly smaller** than `a` and can be obtained by **at most one swap** of two indices. If no such array exists, return `a` unchanged. **Constraints:** in-place, `O(n)` or `O(n log n)`; clearly discuss corner cases (duplicates, already smallest permutation, repeated values near the pivot).

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

Given a string `s`, return the length of the longest contiguous substring that contains no repeated characters. Use a sliding window: keep the index of the last occurrence of each character; when you encounter a repeat inside the current window, move the window start to just past that previous occurrence. Track the maximum window length seen. **Example:** `s = "abcabcbb"` -> `3` ("abc"). `s = "bbbbb"` -> `1` ("b"). `s = "pwwkew"` -> `3` ("wke"). Return `0` for the empty string.

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

  1. Maintain a window [start, i]; remember the last index where each character appeared.
  2. When a character repeats inside the window, jump start to last[ch] + 1 instead of resetting to 0.
  3. Update the answer with the current window length at every step.

K Closest Points to the Origin

Given an integer `k` and a list of points `points` where each point is `[x, y]`, return the `k` points closest to the origin measured by squared Euclidean distance `x*x + y*y`. Use a max-heap of size `k` over squared distances so it never grows beyond `k`, giving `O(n log k)` time. Any ordering of the k results is accepted; this reference returns them sorted ascending by squared distance so the output is deterministic. **Example:** `k = 2`, `points = [[3,3],[5,-1],[-2,4]]` -> `[[3,3],[-2,4]]` (distances 18 and 20, vs 26 for [5,-1]).

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

  1. Comparing squared distances avoids floating point and preserves ordering.
  2. Keep a max-heap of size k (store negated distance) so the farthest of the current best k is at the top.
  3. 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

Given an array of positive integers `values` (each may be reused unlimited times) and an integer `amount`, return the minimum number of values whose sum equals `amount`. Return `-1` if no combination sums to `amount`. This is the unbounded coin-change minimization. Let `dp[a]` be the fewest items summing to `a`; initialize `dp[0] = 0` and everything else to infinity, then for each `a` relax with every value `v <= a` via `dp[a] = min(dp[a], dp[a-v] + 1)`. **Example:** `values = [1,2,5]`, `amount = 11` -> `3` (5+5+1).

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

  1. Define dp[a] = minimum number of values summing exactly to a.
  2. Base case dp[0] = 0; initialize the rest to a sentinel larger than any valid answer (amount + 1).
  3. 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

Given strings `s` and `p`, return all starting indices `i` such that `s[i : i+len(p)]` is a permutation (anagram) of `p`, in ascending order. Use a fixed-size sliding window of length `len(p)` with character-count arrays. Slide one character at a time, adding the entering character and removing the leaving one, and record the start index whenever the window's counts equal `p`'s counts. **Example:** `s = "cbaebabacd"`, `p = "abc"` -> `[0, 6]` ("cba" at 0, "bac" at 6).

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

  1. Build a frequency array for p; maintain a frequency array for the current window of length len(p).
  2. When the window index passes len(p), decrement the count of the character that just left the window.
  3. 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

Given an integer array `a`, return the lexicographically largest array that is strictly smaller than `a` and obtainable by at most one swap of two indices. If no such array exists (a is already the smallest arrangement of its prefix structure), return `a` unchanged. Scan from the right for the first index `i` with `a[i] > a[i+1]` (a descent). Then to its right find the largest value strictly less than `a[i]`; to keep the result as large as possible, swap with the leftmost occurrence of that value when it is duplicated. **Example:** `a = [1,9,4,6,7]` -> `[1,7,4,6,9]`. `a = [1,1,5]` -> `[1,1,5]` (already smallest).

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

  1. 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.
  2. Swap a[i] with the largest value to its right that is strictly less than a[i] to maximize the result while still decreasing.
  3. 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).
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Sum of Weekly Maximum Costs - Salesforce
  • Solve Two OA Coding Problems - Salesforce (medium)
  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)