Feedback on my first ever coding assessment for a job

I finally made it passed “no” to a step in the job interview process and was given a code assessment. This is the first one I have ever done. It seemed pretty easy, which makes me worry I missed some things. It’s been a few days and I have not heard anything since submitting it.

Looking for feedback on a some of the questions, please:

  1. In the following code, describe anything that seems incorrect or you might suggest changing.
var username = inputValues.username; 
var password = inputValues.password;

if(UserIsAuthorized(username)){ 
  AllowAccess();
} else {
   ShowUnauthorizedPage();
}

My Answer:
The code does not pass in the password to UserIsAuthorized. The UserIsAuthorized function should be checking if the username is correct and also if the password is correct. If not already done so, I would suggest storing the password as a hash rather than the users actual password for security reasons. And for authorization check that the users’ hashed input password equals the stored hash for the users’ password.

The name of ShowUnauthorizedPage is not similar to AllowAccess so it is not as readily apparent access is not allowed. I would suggest renaming ShowUnauthorizedPage to something like DoNotAllowAccess.

  1. In the following code, describe anything that seems incorrect or you might suggest changing.
function ReverseString(str){ 
  var reversedStr = "";
  var modifiedStr = "";

  for (var i = str.length; i>=0; i--){
     reversedStr += str[i];
  }
  return str; 
}

My Answer:
As written the function returns the str argument exactly as passed in - not reversed.
The function should return reversedStr. There is a problem with the for loop. On the first iteration of the loop the variable i will be equal to the length of str and because str is zero based indexed str[i] is undefined. So the first thing added to reversedStr is “undifined”. If “Hello World” is passed in the result is “undefineddlroW olleH”. The first iteration of the loop should start with the length of str minus 1, for example: for (var i = str.length - 1; i >= 0; i–).
The variable modifiedStr is not used and should be deleted.

  1. In the following code, what does the variable ‘result’ evaluate to?
var numbers = [1, 2, 3, 4, 5];
var result = 0;
for (var i = 0; i < numbers.length; i++) {
  result += numbers[i] * 2; 
}

My Answer:
30 after the loop runs. For each iteration of the loop it takes the value from numbers at index i and multiplies that value by 2, the result of which is added to result. As the loop runs result becomes 2, 6, 12, 20, and finally 30.

  1. What does ((2 * 8) - 5) % 2 evaluate to?

My Answer:
It evaluates to 1. 2 * 8 is evaluated first = 16, then 5 is subrtacted = 11, finally 11 % 2 = 1;

  1. In a language of your choice, write a method that accepts an array of integers and returns the sum of the second-lowest and second-highest values from the array.

My Answer:

const sumSecondLowestHighest = (arr) => {
  // sort arr
  const arrSorted = arr.sort( (a, b) => a - b)
  // remove duplicate array values if any 
  const arrUnique = []
  for (let i = 0; i < arrSorted.length; i++) {
    if (i == 0) { 
      arrUnique.push(arrSorted[i])
    } else {
      if (arrSorted[i - 1] != arrSorted[i]) {
        arrUnique.push(arrSorted[i]) 
      }
    } 
  }
  // the second value in arrUnigue is the second lowest and the second to last value is the second highest
  return arrUnique[1] + arrUnique[arrUnique.length - 2]
}
  1. In the language of your choice, write code to parse a value from a string that’s prefixed with the word “Organization”.

Examples:

  • “Organizationdemo” → “demo”
  • “Organizationprod” → “prod”

My Answer:

const parseValue = ( prefix, str ) => {
  if (str.startsWith(prefix)) {
    return str.slice(prefix.length)
  } else {
    return str
  }
}
// usage
console.log(parseValue("Organization", "Organizationdemo")) // demo 
console.log(parseValue("Organization", "Organizationprod")) // prod 
console.log(parseValue("Organization", "Organizeprod")) // Organizeprod
  1. Given these tables, would the following two queries return the same results? Why or why not?
    Screen Shot 2022-12-09 at 1.29.42 PM

My Answer:
The results will be different.
Query 1 will return all people even if they do not have an address. A LEFT JOIN gets all rows from the left table, but only rows from the right table that are linked to from the left one.
Query 2 will only return people with an address. Because a LEFT JOIN gets all rows from the left table, but only rows from the right table that are linked, Jane will not be returned because there is no PersonID in the Address table that is linked to Jane.

Thank you for your time!

I suppose you could suggest that they use const instead of var, and make use of destructuring:

const { username, password} = inputValues;

I think this is a little messy. Since you know that you are always going to include the first value in arrSorted you could just do that when you declare arrUnique:

const arrUnique = [arrSorted[0]];

Then the body of your for loop is just an if statement. But an even cleaner way to do this might be to use a Set to throw out all the duplicate numbers in arr, and then sort arr. Also, the sort method does not create a new array but rather returns a reference to the original array since the array is sorted in place. Using a Set will make a copy of the original array, which is probably a good idea. Finally, you probably don’t need to pass in a comparison function to sort since the order they are sorted in doesn’t matter.

const sortedUniqueArray = Array.from(new Set(arr)).sort();

With this as the first line of the function then you only need the return statement. Since the instructions didn’t tell you what to return if the array passed in is empty or has less than three unique numbers then I’m going to assume that they weren’t expecting you to take that into account.

1 Like

Yes, I can see that now. I thought about not using if (i == 0) and just setting arrUnique to the first value in arrSorted and for some reason decided not to, but it would have been much cleaner.

The points about using Set and sort and destructuring are good as well, cleans things up.

I didn’t think about that, it is a good catch though. Because I wrote it in JavaScript I did think about first checking to see if arr is an array, but kind of for the same reason the way the question was asked I decided not to.

Thanks for the reply!

Hey @eoja, I just want to correct this and say that you should keep the comparison function because default sort in JS is just terrible and it should probably never be relied upon (at least when numbers are concerned). Sorry about that terrible advice.

I didn’t think about it when I read your comment, but I have run into it before sorting numbers. Because if there is no comparison function the numbers are converted to strings and will not be sorted as expect for numbers.

Examples for anyone reading this: