JS Interview Question for Mid Level Front End, too hard?

I’ve been interviewing people for a mid level React Front End roll with some backend work. Asking a handful basic and advance JS question. At the end I give them a living coding problem and thirty minutes to complete. They can use any resources other than asking a someone, i.e. anything they can find on google. They have 30 minutes to complete. So far no one has successful completed it. That said they also did poorly on the questions which I just googled up and added a few of my own.

Here is the coding challenge. Is this too hard for someone expected to have 3+ years of JS and 2+ of React?

const maxLineLength = 78
const sampleMedicalText = 'REASON FOR STUDY: 30 Y/O M W/ ACUTE NONDISPLACED FX OF LEFT RADIAL HEAD.\r\n\r\nImages: 3 views of the left elbow.\r\n\r\nPriors: None.\r\n\r\nThere is a nondisplaced fracture of the left radial head/neck. No bridging callus formation is identified. The joint spaces are preserved.  There is no cortical destruction.\r\n\r\nImpression: Nondisplaced left radial head/neck fracture.'
const sampleResult = `
REASON FOR STUDY: 30 Y/O M W/ ACUTE NONDISPLACED FX OF LEFT RADIAL HEAD.

Images: 3 views of the left elbow.

Priors: None.

There is a nondisplaced fracture of the left radial head/neck. No bridging
callus formation is identified. The joint spaces are preserved.  There is no
cortical destruction.

Impression: Nondisplaced left radial head/neck fracture.`.trim()

const medicalText = "REASON FOR STUDY: 38 YOF RUNNER WITH R HIP AND FEMUR PAIN AND HX OF CALCIUM DEFICIENCY.\r\n\r\nORDERING PHYSICIAN:  LCDR DOE  CALLBACK: 555-123-4567\r\n\r\nImages: 4 views of the pelvis.\r\n\r\nPriors: None.\r\n\r\nThere is no acute fracture or dislocation. The joint spaces are preserved.  There is no cortical destruction. Subcentimeter pelvic calcifications are noted which may represent phleboliths.\r\n\r\nImpression: No acute fracture or dislocation."

/**
 * Splits text `str` on \r?\n and then ensure each line has a max length of `length` splitting any line as needed then rejoining all lines with \r\n and returning the result as a string.
 * @param {string} str
 * @param {number} length
 * @return {string}
 */
function medicalTextSplitter(str, length) {

    return // todo
}

console.log(medicalTextSplitter(medicalText, maxLineLength))
1 Like

Gracias, no estoy a la altura, pero me sirve para saber que me voy a encontrar más adelante

I don’t know, I solved it. I was also joining the Office Hours meeting, so I didn’t time myself that well, and maybe it wasn’t the best way to solve it… but I did it

function medicalTextSplitter(str, length) {
    const arr = str.split(/\r?\n/);
    if (arr.every(x => x.length <= length)) return str
    const shortened = arr.map(x => {
      if (x.length <= length) return x;
      const spl = x.split(' ');
      let str = spl[0];
      let i = 1;
      while ((str + ' ' + spl[i]).length <= length) {
        str += ' ' + spl[i];
        i++;
      }
      
      return str + '\r\n' + spl.slice(i).join(' ')
    })
    return medicalTextSplitter(shortened.join('\r\n'), length)
}
function medicalTextSplitter(str, length) {
    const firstSplit=str.split(/\r?\n/);
    const secondSplit=[];
    for (item of firstSplit){
      if (item.length<=length){
        secondSplit.push(item);
      }
      else{
        const temp = item.split(" ");
        let curr = "";
        while (temp.length>0){
          if (curr.length+temp[0].length>length){
            secondSplit.push(curr.trim());
            curr="";
          }
          else if (temp[0].length>length){
            secondSplit.push(temp.shift());
          }
          else{
            curr+=temp.shift()+" ";
          }
        }
      }
    }
    return secondSplit.join('\r\n');
}

I think that does what you want? took me a good 15 minutes, but I had a very clear idea of what I wanted to do, it was mostly typing it up. Idk, I’ve only really taken to coding the past year.

I don’t think this challenge is that hard, seems like a level 6-7 on codewars, which someone with a few years coding experience should be able to solve. And the instructions seem very clear to me. One thought would be that perhaps they aren’t familiar with the carriage return. Granted, you are giving them the pattern to split on and telling them to end each line with \r\n but in an interview setting with a time limit sometimes little details like this can throw you off.

I’m just guessing here though. Do you get to see their coding attempt? If so, how close do they usually get? What do you think is the stumbling block here?

One other thought, perhaps name the second parameter maxLength instead of just length? I’m grasping at straws here for sure but again, when people are stressed out then little details like these can throw people off.

I started typing this yesterday, lol.

@ilenia @MatchaCrisp both of you are spot on. @bbsmooth I do agree with you and tell them they are free to ask me questions about the requirement. I also point out that the \r?\n is regex. I should have been more clear about that. Today was the first interview that was able to complete it. He started out very strong but took a wrong turn. Once he struggled for a while I asked him why don’t you use the same sort of pattern for each line as you did for the entire body? That was enough to get him back on track. He even managed to surprise me with a few cleaver ideas. I have two more interviews to do so will see if anyone is able to do it with out any help.

Here is the solution I came up with in about five minutes with another two to three minutes of debugging.

function medicalTextSplitter(str, length) {
    let lines = str.split('\r\n')
    const out = []
    let newLine = ''

    lines.forEach(line=>{
        line.split(' ').forEach(word=>{
            if (newLine.length + word.length < length) {
                newLine += `${word} `
            } else {
                out.push(newLine.trim())
                newLine = `${word} `
            }
        })
        out.push(newLine)
        newLine = ''
    })
    return out.join('\r\n')
}

Here is the code from our utilities lib based on a Stack Overflow question.

function splitOnSpace(str, length, noTrim) {
    const regex = new RegExp("(.{1," + length + "}(\\s|$))\\s*", 'g')
    const matches = String(str).match(regex) || ['']
    return matches.map((ele) => (noTrim) ? ele : ele.trim())
}

Note this solution does not work for this specific problem but is what the problem is based on. It’s a much more complex problem where input text is reformatted and the number of line breaks between sections is set to two no matter what the original input was. Just figure there may be some interests. I always try to base live coding and projects on stuff we do in production so they can get a feel for what we do.

1 Like

It might be possible that your candidates clamped up because they wanted to try and come up with the most “optimal” or “clever” solution to impress you.
But instead ended up overthinking it and failing the challenge. :grinning:

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