Help With Rosetta Code First/Last Letter

Coding Challenge - Last letter-first letter

For this Rosetta coding challenge, I was able to write the JS code which gives the output exactly as required (an array with words, where the first letter of each word is the same as the last letter of the previous word).

But the tests are failing on the interface. Can’t figure out where I need to make the changes because on running the same code on VS Code, the output looks exactly as required.

I can’t figure out where you need to make the changes without seeing your code and a link to the challenge :upside_down_face:

Absolutely Sir! Here is the code.

function indexCombination(wordList,indexStrF, lastF,removedIndexF = [], inputStrInListF =[])
{

    let i = 0;
    while (i <= wordList.length - 1)
    {
        let flag = 0;
        if (!removedIndexF.includes(String(i)) && !indexStrF.includes(String(i)) && !inputStrInListF.includes(String(i)))
        {
            let first1 = wordList[i][0];
            let wordFirstIndex = i;

            if (first1 == lastF)
            {
                indexStrF.push(String(wordFirstIndex));
                lastF = wordList[i][wordList[i].length-1];
                flag = 1
                i = 0
                removedIndexF.length =  0 ;
            }
        }
        if (flag == 0)
            i += 1;
    }
    if (removedIndexF.length > 0)
        removedIndexF.length = 0;

    return indexStrF;
}

function morecombinations(wordList, inputStr, wordSeqIndex, defaultList=[])
{
    let inputStrInList = inputStr.split(";");
    let inputStrInListCopy = [...inputStrInList];
    let removedIndex = [];
    let combineList = defaultList;

    while (inputStrInList.length > 1)
    {
        removedIndex.push(inputStrInList[inputStrInList.length-1]);
        inputStrInList = inputStrInList.slice(0,inputStrInList.length-1);
        indexStr1 = [...inputStrInList];
        last = wordList[inputStrInList[inputStrInList.length-1]][wordList[inputStrInList[inputStrInList.length-1]].length-1];
        
        indexStr1 = indexCombination(wordList,indexStr1, last,removedIndex, inputStrInList);

        let myStr = indexStr1.join(";");
        //if (indexStr1.length >=2 && !combineList.includes(myStr) && !wordSeqIndex.includes(myStr))
        if (indexStr1.length >= inputStrInListCopy.length && !combineList.includes(myStr) && !wordSeqIndex.includes(myStr))
            combineList.push(myStr);
    }

    if (combineList.length != 0)
    {
        wordSeqIndex.push(combineList[0]);
        let listStr = combineList[0];
        combineList.shift();
        let someList = [...combineList];
        morecombinations(wordList, listStr, wordSeqIndex, someList);
    }

    //return True

}

function findLongestChain(items) 
{
    let indexList = [];

    items.sort();

    items.forEach((x, index) => {

        let last = x[x.length-1];
        
        let indexStr = [];
        indexStr.push(String(index));

        indexStr = indexCombination(items, indexStr, last);

        if (indexStr.length > 1)
        {
            let strIndex = indexStr.join(";");
            indexList.push(strIndex);
            morecombinations(items, strIndex, indexList);
        }
    });
   
    let longSeq = [];

    if (indexList.length > 0)
    {
        let maxLength = indexList[0].split(";").length;
        let index1 = 0;

        indexList.forEach((ele, index) => {
            if (ele.split(";").length > maxLength) 
            {
                maxLength = ele.split(";").length;
                index1 = index;
            } });

        indexList[index1].split(";").forEach((ele,index) =>{
            longSeq.push(items[ele]); });
    }

    return longSeq;

}

```

which challenge is this?

Edit, found it: https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/last-letter-first-letter

ReferenceError: assignment to undeclared variable indexStr1

It looks like you have un-declared variables that you need to fix.

Coding Interview Challenge - Rosetta Code - Last letter-first letter

Link is https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/last-letter-first-letter

Yes. You expected others to go play hide and go seek to find the challenge and start debugging your code. That makes it harder for others to help you.

I am so sorry for not providing all the info as you requested initially. Couldn’t gather my thoughts initially when I started replying to you. Lack of focus on my side.

It happens, I was just explaining why I was asking for it.

Did you see this:


Running the test cases is my first debugging step. This is the first error message that comes up.

I had to stop working right after my last message because of severe thunderstorm. You were spot on about the un-declared variables in my code.

It now passed all the test cases.

Appreciate your support! :slight_smile: .

I should admit that all the coding challenges here are challenging / demanding. It demands one to be thorough with the fundamental knowledge of a programming language and also algorithms and data structures.

To be honest, it took me hours (and days) to come up with a solution and I still believe there could be a better approach to solve this but I can’t expand my thought process into that. Average person like me can’t solve these problems within the limited time frame of an interview session. Still, the objective is to learn and improve.

If your interviewer and interview process is reasonable, getting a tight, perfect, optimized solution isn’t the point. It’s about showing how you dig into the problem and break in down into pieces that you start solving.

Thank you for the feedback. You being an expert and with vast experience in the software industry, just wanted more insight from you about your approach / strategy to problem solving .

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.