Find longest subsequence in string
Last updated: February 22, 2026
Quick Overview
Given a string, write a function to find the longest subsequence that can be derived from the string, where a subsequence is defined as a sequence that can be obtained by deleting some characters without changing the order of the remaining characters. The function should return the length of this longest subsequence. For example, for the input "abcde", the longest subsequence is "abcde" itself, with a length of 5.
Adobe
February 22, 202638
9
1,984 solved
Given a string, write a function to find the longest subsequence that can be derived from the string, where a subsequence is defined as a sequence that can be obtained by deleting some characters without changing the order of the remaining characters. The function should return the length of this longest subsequence. For example, for the input "abcde", the longest subsequence is "abcde" itself, with a length of 5.
This coding problem is frequently asked during Onsite at Adobe. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Adobe expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
- Discuss multiple approaches and trade-offs before coding
- Implement an optimal solution with clean, production-quality code
- Handle all edge cases including boundary conditions and invalid input
- Optimize both time and space complexity with clear justification
- Test your solution systematically with well-chosen examples
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the input doesn't fit in memory?
- Can you solve this in a single pass?
- How would you test this solution thoroughly?
- Can you optimize the space complexity of your solution?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To find the longest subsequence in a given string, we can recognize that the problem is about maintaining the order of characters while potentially skipping others. A subsequence allows characters to ...
Approach
We will use a dynamic programming approach to solve this problem. The idea is to maintain an array, dp, where dp[i] represents the length of the longest subsequence that ends with the character at...