PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of dynamic programming and sequence comparison algorithms by asking for the length of the longest common subsequence between two strings.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Find longest common subsequence length

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given two strings `s` and `t`, compute the length of their **longest common subsequence** (LCS). A subsequence is obtained by deleting zero or more characters from a string without changing the relative order of the remaining characters. ## Input - Two strings `s` and `t` (may be empty) ## Output - An integer: the maximum length of any common subsequence of `s` and `t` ## Example - `s = "abcde"`, `t = "ace"` → output `3` ("ace") ## Constraints (you may assume) - `0 <= len(s), len(t) <= 2000` - Characters are ASCII letters (or general bytes).

Quick Answer: This question evaluates understanding of dynamic programming and sequence comparison algorithms by asking for the length of the longest common subsequence between two strings.

Given two strings s and t, return the length of their longest common subsequence (LCS). A subsequence is formed by deleting zero or more characters from a string without changing the relative order of the remaining characters. Note that a subsequence does not need to be contiguous. For example, in the strings "abcde" and "ace", the longest common subsequence is "ace", so the answer is 3.

Constraints

  • 0 <= len(s), len(t) <= 2000
  • Strings may be empty
  • Characters are ASCII letters or general bytes
  • An O(len(s) * len(t)) dynamic programming solution is acceptable

Examples

Input: ("abcde", "ace")

Expected Output: 3

Explanation: "ace" is a common subsequence of both strings, and no longer common subsequence exists.

Input: ("abc", "abc")

Expected Output: 3

Explanation: The entire string matches, so the LCS length is 3.

Input: ("abc", "def")

Expected Output: 0

Explanation: The two strings share no common characters, so the longest common subsequence has length 0.

Input: ('', 'abc')

Expected Output: 0

Explanation: If one string is empty, there cannot be any common subsequence other than the empty one.

Input: ("aggtab", "gxtxayb")

Expected Output: 4

Explanation: One longest common subsequence is "gtab", which has length 4.

Input: ("aaaa", "aa")

Expected Output: 2

Explanation: Repeated characters still count in order; the longest common subsequence is "aa".

Hints

  1. Think about prefixes: what is the answer for s[:i] and t[:j] based on smaller prefixes?
  2. If the current characters match, you can extend a smaller subsequence; otherwise, you may need to skip one character from either string.
Last updated: May 11, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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
  • 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

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)