Longest Increasing Subsequence

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

The Salesforce OA reported in July 2026 asks for Longest Increasing Subsequence, and the all-sevens example is where careless code dies. Strictly increasing means equal values don't count, so [7,7,7,7,7,7,7] returns 1, not 7. If you've seen this one before, you know the O(n^2) DP works. If you blank, you're stuck staring at a classic you half remember. This page gives you the pattern and the trap before you sit down. StealthCoder sits invisibly on your screen during the live assessment as a safety net if your mind goes empty mid-problem.

The problem

Given an integer array nums, return the length of its longest strictly increasing subsequence.
A subsequence is formed by deleting zero or more elements without changing the relative order of the remaining elements.

Function
lengthOfLIS(nums: int[]) → int

Examples
Example 1
nums = [10,9,2,5,3,7,101,18]
return = 4
One longest strictly increasing subsequence is [2, 3, 7, 101], which has length 4.
Example 2
nums = [0,1,0,3,2,3]
return = 4
The subsequence [0, 1, 2, 3] is strictly increasing and has the maximum possible length.
Example 3
nums = [7,7,7,7,7,7,7]
return = 1
Equal values cannot both appear in a strictly increasing subsequence, so the maximum length is 1.

Constraints
1 <= nums.length <= 2500
-10^4 <= nums[i] <= 10^4

Reported by candidates. Source: FastPrep

Pattern and pitfall

The standard approach is DP. Let dp[i] be the length of the longest strictly increasing subsequence ending at index i. Start every dp[i] at 1, then for each j less than i, if nums[j] < nums[i], set dp[i] = max(dp[i], dp[j] + 1). Answer is the max over all dp values, not dp[n-1]. With n up to 2500, O(n^2) is fine. The faster version keeps a tails array and uses binary search to place each number, giving O(n log n). The pitfall is the comparison. Use lower bound (first element >= x) so equal values replace instead of extend. Using <= in the DP also breaks Example 3. If you freeze on the live OA, StealthCoder can surface the working solution so you can check it against the three examples.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Longest Increasing Subsequence cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest increasing subsequence. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Increasing Subsequence FAQ

How hard is Longest Increasing Subsequence really?+

Medium. The O(n^2) DP is short and well known. Most failures come from returning dp[n-1] instead of the max, or treating equal values as increasing. With n capped at 2500, the quadratic solution is enough to pass.

What's the trick for this Salesforce OA question?+

Define dp[i] as the best length ending at i, and only extend from earlier values strictly smaller than nums[i]. Take the max across all i. That one definition handles every example, including the all-equal array.

Do I need the O(n log n) solution?+

Probably not. The constraint is 2500, so O(n^2) is about 6 million operations. Know the binary search tails version as a backup, but write the DP first because it's easier to get right under pressure.

What edge case breaks a naive solution?+

Duplicates. In [7,7,7,7,7,7,7] the answer is 1 because strictly increasing rejects equal values. If you use <= instead of <, you'd return 7. Also a single-element array should return 1.

How do I prepare in 48 hours?+

Write the O(n^2) DP from scratch twice without looking. Run it on the three given examples by hand. Then write the tails plus binary search version once. Focus on the strict versus non-strict comparison, since that's where points get lost.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it