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:
- 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.
- 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.
- 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.
- 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;
- 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]
}
- 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
- Given these tables, would the following two queries return the same results? Why or why not?
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!