Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to generate all combinations of a string with a given length.

Hard

Problem Statement

Write a function to generate all combinations of a string with a given length.

Sample Input

str = "abc", length = 2

Sample Output

[ 'ab', 'ac', 'bc' ]

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Generate all combinations of a string with a specific length function getCombinations(str, length, start = 0, current = '', combinations = [ ]) { // Check if current combination has the required length // Loop through characters, recursively forming combinations } console.log(getCombinations("abc", 2));
Log in or sign up to run or submit your code.Log inSign up
Input
str = "abc", length = 2
Expected Output
[ 'ab', 'ac', 'bc' ]