BackmediumRecursionInfosys

Galactic Navigation Sequences Solution

Problem Statement

Given an integer n, produce every possible navigation sequence of length 2*n that contains exactly n hyperjumps denoted by 'J' and n hyperslows denoted by 'S'. A sequence is valid only if, while scanning from left to right, the count of 'J' never falls below the count of 'S' at any position. Return all valid sequences as an array of strings; the order of strings is irrelevant.

Example 1
Input
1
Output
["JS"]

Explanation: With n=1 we need one J and one S. The only ordering that never has more S than J in a prefix is J followed by S, yielding the single sequence "JS".

Example 2
Input
2
Output
["JJSS","JSJS"]

Explanation: n=2 requires two J and two S. The two arrangements that keep J count >= S count at every prefix are: 1) JJSS (J,J,S,S) and 2) JSJS (J,S,J,S). Any other ordering violates the prefix rule.

Example 3
Input
3
Output
["JJJSSS","JJSSJS","JJSJSS","JSJJSS","JSJSJS"]

Explanation: n=3 produces five Catalan-number sequences. Each string contains three J and three S and respects the prefix condition. For example, "JJSSJS" is built as J,J,S,S,J,S; at every step the number of J's is never less than the number of S's.

Constraints

  • 1 <= n <= 10
  • The total number of returned strings equals the nth Catalan number, which grows roughly as O(4^n/(n^{3/2}))
  • Memory usage must accommodate all generated strings
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Galactic Navigation Sequences — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(C_n) where C_n is the nth Catalan number (≈4^n/(n^{3/2}))
|
SpaceO(n) auxiliary recursion stack plus O(C_n·n) for the output

Problem Description

Given an integer n, produce every possible navigation sequence of length 2*n that contains exactly n hyperjumps denoted by 'J' and n hyperslows denoted by 'S'. A sequence is valid only if, while scanning from left to right, the count of 'J' never falls below the count of 'S' at any position. Return all valid sequences as an array of strings; the order of strings is irrelevant.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galactic Navigation Sequences"

medium

WHY DOES IT MATTER?

Balanced sequence generation appears in parsing, compiler design, and concurrency control where well‑formed nesting is mandatory; mastering this pattern teaches you to prune exponential search spaces efficiently.

OPTIMIZATION CHALLENGE

The key insight is to track the remaining quota of each symbol and enforce the invariant JCount≥SCount at every step, which collapses an exponential 2^{2n} space into the Catalan‑size solution space.

REAL-WORLD CONNECTION

Think of a distributed transaction log where every 'J' opens a sub‑transaction and every 'S' commits it; the system must never commit more sub‑transactions than it has opened, mirroring the balance constraint.

During an interview, write the recursive helper with clear parameters (pos, jUsed, sUsed, current) and immediately return when jUsed>n or sUsed>jUsed – this shows you understand pruning and avoids unnecessary branching.

COMPLEXITY AT A GLANCE

⏱ Time:O(C_n) where C_n is the nth Catalan number (≈4^n/(n^{3/2}))
💾 Space:O(n) auxiliary recursion stack plus O(C_n·n) for the output

Core Theory — Why This Approach?

The problem is a classic generation of balanced parentheses where 'J' plays the role of '(' and 'S' of ')'. The set of all valid sequences of length 2n corresponds to the nth Catalan number, which grows roughly as 4^n/(n^{3/2}√π). A naive enumeration that builds every 2n‑character string and then filters by the balance rule requires O(2^{2n}) time and quickly becomes infeasible. The optimal paradigm uses recursive backtracking with two counters – the number of J's placed and the number of S's placed – and enforces the invariant that at any recursion depth JCount≥SCount. This pruning eliminates entire sub‑trees that would violate the rule, guaranteeing that only Catalan‑many nodes are visited. The recursion naturally mirrors a depth‑first traversal of a binary decision tree, and because each recursive call adds a single character, the algorithm runs in O(C_n) time and uses O(n) auxiliary stack space besides the output container.

Interview Questions on This Problem

Q1How would you modify the backtracking solution to generate the sequences in lexicographic order?

Maintain the same recursion but always try adding 'J' before 'S'. Since 'J' < 'S' in ASCII, this depth‑first order yields lexicographically sorted results without extra sorting.

Q2What is the relationship between this problem and the Catalan numbers, and how can that be used to verify your solution’s correctness?

The count of valid sequences for a given n equals the nth Catalan number C_n = (2n choose n)/(n+1). After implementing the generator, you can assert that the length of the returned array matches C_n for several n values as a sanity check.

Q3If the input n can be as large as 15, what practical considerations affect your implementation in a production interview setting?

Even for n=15, C_15 = 9694845, which may exceed memory limits; therefore you should discuss streaming the results (e.g., using a generator/yield) or limiting n, and emphasize that the algorithm’s asymptotic cost is optimal for exhaustive generation.

Examples

Example 1

Input

1

Output

["JS"]

Explanation: With n=1 we need one J and one S. The only ordering that never has more S than J in a prefix is J followed by S, yielding the single sequence "JS".

Example 2

Input

2

Output

["JJSS","JSJS"]

Explanation: n=2 requires two J and two S. The two arrangements that keep J count >= S count at every prefix are: 1) JJSS (J,J,S,S) and 2) JSJS (J,S,J,S). Any other ordering violates the prefix rule.

Example 3

Input

3

Output

["JJJSSS","JJSSJS","JJSJSS","JSJJSS","JSJSJS"]

Explanation: n=3 produces five Catalan-number sequences. Each string contains three J and three S and respects the prefix condition. For example, "JJSSJS" is built as J,J,S,S,J,S; at every step the number of J's is never less than the number of S's.

Constraints

  • 1 <= n <= 10
  • The total number of returned strings equals the nth Catalan number, which grows roughly as O(4^n/(n^{3/2}))
  • Memory usage must accommodate all generated strings

Optimal Approach & Strategy

Use backtracking with two counters, adding 'J' while jCount<n and adding 'S' only when sCount<jCount, which directly builds only valid sequences.

Brute Force Approach

Generate all 2^{2n} binary strings of length 2n, then filter those with exactly n J's and n S's and that never let S exceed J while scanning.

Verified Code Solutions

JavaScript Solution
Time: O(C_n) where C_n is the nth Catalan number (≈4^n/(n^{3/2}))
/**
 * @param {number} n
 * @return {string[]}
 */
var generateSequences = function(n) {
    const result = [];
    
    const backtrack = (jRemaining, sRemaining, jCount, sCount, current) => {
        if (jRemaining === 0 && sRemaining === 0) {
            result.push(current);
            return;
        }

        // Add 'J' if we still have jumps remaining
        if (jRemaining > 0) {
            backtrack(jRemaining - 1, sRemaining, jCount + 1, sCount, current + 'J');
        }

        // Add 'S' if we have slowns remaining and count of J is greater than count of S
        if (sRemaining > 0 && jCount > sCount) {
            backtrack(jRemaining, sRemaining - 1, jCount, sCount + 1, current + 'S');
        }
    };

    backtrack(n, n, 0, 0, '');
    return result;
};

// Driver code
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Enter n: ', (n) => {
    n = parseInt(n);
    const result = generateSequences(n);
    result.forEach(seq => console.log(seq));
    rl.close();
});

Asked in Top Tech Interviews

Infosys

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.