1.25 – Flash cards and getting beaten by an interview question.

3h Today I typed up my ruby notes and made a super rough concept I have had for a while. I never actually look at my flash cards. Part of it is that I don’t like reviewing very much when I could be learning or applying knowledge. The other part is that I don’t always have them with me, and I don’t always have the right supplies to be making cards when I want to. I need to get better at forcing spaced repetition so that I can retain some of this info and train myself to learn better once the bootcamp actually starts. The other issue might be able to be solved by making my flash cards electronic. Here’s my idea: make a cli or web app that takes in some data structure that has the questions and answers in it. Display them. Learn them. The cli should be pretty easy. The web app may not, but it could also be a learning tool. I made an array that has strings in the format “question:answer” I am randomly selecting a string from the array, splitting it on the colon, and then displaying it. I then remove it from the array so that I don’t repeat questions. I have it all built in js, but I’m not super sure how to make that a cli app so I may either rewrite in ruby or give it a quick frontend. I’m not sure what makes more sense.

I got whooped on a practice interview question tonight. My brain was a little fuzzy and it took a long time to come up with a barely working solution. I didn’t consider many edge cases and I only ended up passing 2 of 10 test cases. I also took 45 minutes so I got no time points. woof. The problem is: when given a string, see if the removal of up to two characters could enable the string to be a palindrome.

Most of the test cases I failed were related to removing characters from the tail of the string. My solution starts at the head and moves backwards. One of the other test cases required removing the first and last characters. I’m not sure how one would best go about doing that. I could run it backwards once and forwards once. I could also try to find the center and iterate out from there, removing characters as I go. Both ways would require a lot more iterations and a way to compare ‘tries’ to find out if one solution works. I could also do some filtering to see if it is even possible. If you had more than one odd value in the char count you already know it can’t be a palindrome. UGH – I just realized that I had another problem fully reading the problem and I consequently missed out on two cases that would have been easy to code – if the input is already a palindrome. I DID slow down this time, but not enough. Mental note for the next one.

well, just so the world can see my mistakes: here is my barely-functional-in-the-few-cases-I-considered code.

function PalindromeEditor(str){
    let deleteCount = 0
    let removedChars = ""
    let i = 0
    while (i<str.length && str.length > 0 && deleteCount <= 2){
        if (str[i] != str[str.length-i-1])
        {
            removedChars += str[i]
            deleteCount ++
            str = str.slice(0,i)+str.slice(i+1)
        } else {
        i++
        }
    }
    return [str.slice(0,i)+str.slice(i+1),deleteCount,removedChars]
} 

function PalindromeChecker(str){
    try {
        for (let i=0; i<str.length; i++){
            if (str[i] != str[str.length-i-1])
            {
                return false
            } 

        }
              return true
    } 
    catch (e) { 
        return false
    }
}

function PalindromeCreator(str) { 
    results = PalindromeEditor(str) 
      if (results[0].length > 0){
          if (PalindromeChecker(results[0])){
            return results[2]
        } else {
            return "not possible"
        }
      }
      else {
          return "not possible"
      }
}

PalindromeCreator("mmop");

As a palate cleanser from that experience; the photo above is a funny series of error messages I got in console today. Damned if I do, Damned if I don’t 🙂

Tell me what you think.

This site uses Akismet to reduce spam. Learn how your comment data is processed.