MockQuestions

JavaScript Intermediate Level Implementation Mock Interview

To help you prepare for your JavaScript Intermediate Level Implementation interview, here are 9 interview questions and answer examples.

Get More Information About Our JavaScript Intermediate Level Implementation Interview Questions

Question 1 of 9

Find the letter combinations of a phone number.

This question focuses on finding combinations using strings with JavaScript.

You are given a string of digits consisting of digits 2-9 (both inclusive). Your task is to return all possible letter combinations that can be formed by pressing these digits on a keypad mobile. A mapping from digits is given below:
1 2 abc 3 def
4 ghi 5 jkl 6 mno
7 pqrs 8 tuv 9 wxyz

/*Example*/

Given digits = “23”
Expected output:- ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Constraints
0 <= digits <= 4
“2” <= digits[i] <= “9”

Solution:

Approach I (recursive):

We will create all the combinations recursively. We will create our helper recursive function combination. This function adds a new combination to the result. The function takes the following arguments:-
1. digits: The given string digits.
2. result: The resulting array where to which every combination will be stored.
3. KEYS: The mapping of digits to letters.
4. idx: The index of given string digits up to which we will compute a combination of digits.
5. prefix: The prefix of the combination which we have seen up to index idx - 1 of the given string digits.

When the idx is equal to the length of given string digits, it means we have found one possible combination. For given digits = “23”, the recursion stack is (result array and KEYS mapping are omitted in the function calls):
|combination(“23”, 0, “”)
| |digit = “2”
| |letters = “abc”
| |loop on letters “abc”
| | |combination(“23”, 1, “a”)
| | | |digit = “3”
| | | |letters = “def”
| | | |loop on letters “def”
| | | | |combination(“23”, 2, “ad”)
| | | | | |2 == digits.length // true
| | | | | |push “ad” to result
| | | | | |return
| | | | |combination(“23”, 2, “ae”)
| | | | | |2 == digits.length // true
| | | | | |push “ae” to result
| | | | | |return
| | | | | combination(“23”, 2, “af”)
| | | | | |2 == digits.length // true
| | | | | |push “af” to result
| | | | | |return
| | | // similar loop on letters “b” and “c”

/**
 * @param {string} digits
 * @return {string[]}
 */
function letterCombinations(moves) {
    if (digits.length === 0) return []

    const result = []
    const KEYS = [
        "",
        "",
        "abc",
        "def",
        "ghi",
        "jkl",
        "mno",
        "pqrs",
        "tuv",
        "wxyz",
    ]
    
    combination(digits, result, KEYS, 0, “”)
    
    return result
}

function combination(digits, result, KEYS, idx, prefix) {
    if (idx === digits.length) {
        result.push(prefix)
        return
    }
    
    const digit = digits[idx]
    const letters = KEYS[digit]
    
    for (const letter of letters) {
        combination(
            digits,
            result,
            KEYS,
            idx + 1,
            prefix + letter,
        )
    }
}

n = length of the digit string
Time complexity- O(n * 4n)
Space complexity- O(n)

Approach II (iterative):

We will loop over each digit in string digits. For each digit, we will find the corresponding letters using the map KEYS. We will have a final result array. For each digit, we will have a new array newResult. This array will store the letter combination up to the current digit. For each letter for the corresponding digit, we iterate over the previously stored prefixes in the result array. For each such prefix, we push the new combination prefix + letter to newResult. After finding the new combinations up to the current digit, we reassign the result to newResult so that we can iterate over new prefixes for the next digit. After the digits loop, we return the result.

/**
 * @param {string} digits
 * @return {string[]}
 */
function letterCombinations(moves) {
    if (digits.length === 0) return []

    const KEYS = [
        "",
        "",
        "abc",
        "def",
        "ghi",
        "jkl",
        "mno",
        "pqrs",
        "tuv",
        "wxyz",
    ]
    
    let result = [""]
    
    for (let digit of digits) {
        const newResult = []
        digit = parseInt(digit)
        const letters = KEYS[digit]
        
        for (const letter of letters) {
            for (const prefix of result) {
                newResult.push(prefix + letter)
            }
        }
        
        result = newResult
    }   
    
    return result
}

n = length of the digit string
Time complexity- O(n * 4n)
Space complexity- O(1)

Written by on June 27th, 2021

Next Question

9 JavaScript Intermediate Level Implementation Interview Questions & Answers

Below is a list of our JavaScript Intermediate Level Implementation interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Find the letter combinations of a phone number.

  • 2. How do you multiply strings without a leading-zero product?

  • 3. Use minimum operations to make an array equal.

  • 4. Find the user's active minutes.

  • 5. What is the score of parentheses with the given string?

  • 6. Sort characters by frequency.

  • 7. Find all duplicates in an array.

  • 8. What are the minimum number of jumps to get to index 0?

  • 9. Find the maximum length of consecutive ones.