FastPrepCount Valid Sequences
Problems/Count Valid Sequences

Count Valid Sequences

OpenAI logoOpenAI● MediumOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Given n and a list of pairs of numbers that are forbidden to be in the same sequence, provide the number of possible sub-sequences (of any size) with consecutive numbers from 1 to n. Example: for n=4 and pairs=(1,3) there are 8 valid sequences are (1),(2),(3),(4),(1,2),(2,3),(3,4),(2,3,4) with (1,2,3),(1,2,3,4) being invalid as they contain a forbidden pair.

Function

countValidSequences(n: int, pairs: int[][]) → int

Examples

Example 1

n = 4pairs = [[1, 3]]return = 8

The valid sequences are (1),(2),(3),(4),(1,2),(2,3),(3,4),(2,3,4). Sequences (1,2,3) and (1,2,3,4) are invalid as they contain the forbidden pair (1,3).

More OpenAI interview questions

  • Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
  • Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
  • Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
  • Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
  • Count Routes Through Four Shop TypesOA · Seen Oct 2026

Similar questions at other companies

  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
  • Maximum Points by Deleting ElementsIBM · OAIBM interview questions
  • Longest String ChainVisa · OAVisa interview questions
  • Maximum Points by Deleting ElementsWalmart · OAWalmart interview questions
  • Bitwise XOR SubsequencesJP Morgan Chase · OAJP Morgan Chase interview questions
  • Count Equal-Frequency SubsequencesExpedia · OAExpedia interview questions
  • Bitwise XOR SubsequencesGoldman Sachs · OAGoldman Sachs interview questions
  • Longest String ChainLinkedIn · OALinkedIn interview questions
  • Max LikesTikTok · OATikTok interview questions
  • Find the Maximum Length of a Good Subsequence I (LC 3176 :)Snowflake · OASnowflake interview questions

Interview reports that mention this question

  • OpenAI OA: What It Tests (2026)
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public int countValidSequences(int n, int[][] pairs) {
      // write your code here
    }

}
Checking account…