Find longest subsequence in string

Last updated: May 15, 2026

Quick Overview

Given a string, write a function to find the longest subsequence that can be formed by deleting some characters without changing the order of the remaining characters. The function should return the length of this longest subsequence. For example, in the string "abcde", the longest subsequence is "abcde" itself, with a length of 5.

xAI
Coding & Algorithms
Machine Learning Engineer
xAI
May 15, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Easy

103

5

3,000 solved


Given a string, write a function to find the longest subsequence that can be formed by deleting some characters without changing the order of the remaining characters. The function should return the length of this longest subsequence. For example, in the string "abcde", the longest subsequence is "abcde" itself, with a length of 5.

Coding interviews at xAI focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Data structure selection and trade-offs
Time and space complexity analysis
Binary search and divide and conquer
Tree structures and recursion
Graph algorithms and traversal
Sorting and searching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you modify your solution to handle streaming input?
  • What is the worst-case input for your solution?
  • Can you solve this iteratively instead of recursively (or vice versa)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To solve the problem of finding the longest subsequence in a string, we need to recognize that a subsequence is formed by deleting some characters from the string while maintaining the order of the re...

Approach
  1. Initialize a DP array of the same length as the input string, with all values set to 1, because the minimum length of a subsequence containing at least one character is 1.
  2. Iterate through the st...

Submit Your Answer
Markdown supported

Related Questions