Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save srdone/1e028ea4267e4aca73f2 to your computer and use it in GitHub Desktop.
Save srdone/1e028ea4267e4aca73f2 to your computer and use it in GitHub Desktop.
Interview Zen Interview Questions for Simplifile
See videos of process at http://www.interviewzen.com/interview/ZdS2ns
//Question:
// Given an array of integers, write a function that will return a boolean that determines if the array is sorted, lowest to highest.
// Function should return true or false in the following cases:
// [-1, 0, 1]: return true (successful test)
// [-1, 3, 10, 10]: return true (successful test)
// [1, 1, 1]: return true (successful test)
// [0, -1]: return false (successful test)
// [0]: return true (successful test)
// [-1, -3]: return false (successful test)
// []: return true (successful test)
// [-1, 0, 1, 10, 9]: return false (successful test)
var isSorted = function (arrayToTest) {
if (arrayToTest.length <= 1) { // take care of cases where array is empty or has one element
return true;
}
for (var i = 1; i < arrayToTest.length; i++) {
if (arrayToTest[i - 1] > arrayToTest[i]) { // if the array has any places where the next element is smaller, it is not sorted lowest to highest
return false;
}
}
return true; // if we got through the loop, everything should be sorted properly
}
////////Now Testing in Console/////////////
// some additional items to consider:
// We could accept a callback with a custom function to determine if it is sorted as expected
// But that function might be just as simple as this, something to think about more though.
// Question:
// Given a string containing a set of words separated by whitespace, we would like to transform it to a string in which the words appear in the reverse order.
// For example, "Alice likes Bob" transforms to "Bob likes Alice". We do not need to keep the original string.
// Tests to pass:
// 'Frank' returns 'Frank' (test passed)
// '' returns '' (test passed)
// 'Alice likes Bob' returns 'Bob likes Alice' (test passed)
// initially, we will ignore punctuation. So the following will be initial results:
// 'Alice, do you like Bob?' returns 'Bob? like you do Alice,' (test passed)
// ',' returns ',' (test passed)
// ', ?' returns '? ,' (test passed)
var reverseWords = function (stringToReverse) {
var splitString = stringToReverse.split(' '); //split only on space
var reversedStringArray = [];
// reverse the array
for (var i = 0; i < splitString.length; i++) {
reversedStringArray.unshift(splitString[i]);
}
// array looks fine, want to test the join now
var reversedString = reversedStringArray.join(' '); // return joined string from reversed array
return reversedString;
}
//////// TESTING in console ////////
// Future versions might include handling of punctuation
//Question:
//Write a program that outputs the first 6 lines of Pascal's Triangle.
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//1 5 10 10 5 1
//Each row begins and ends with the number one. The remaining numbers are obtained by summing the two numbers that lie directly above that number on the previous row and the number that preceeds it. So, in order to find the numbers in the nth row of the triangle, we need the values of the (n-1) the row. Let's say that we have computed the fourth row - 1 3 3 1. Now, the fifth row has five elements. The first and the last element would be one. The remaining elements would be (1+3), (3+3), (3+1) = 4, 6, 4. So, the complete fifth row would be 1 4 6 4 1.
// A generic pascal's triangle function would accept a number of lines, and pass the following tests:
// (in all cases below, we are just showing the last row, but a correct result would include all prior rows)
// 1 returns [1] (passed)
// 2 returns [1, 1] (which is result of [1, 1]) (passed)
// 3 returns [1, 2, 1] (which is result of [1, (1 + 1), 1] (passed)
// 4 returns [1, 3, 3, 1] (which is result of [1, (2+1), (1+2), 1]) (passed)
// 5 returns [1, 4, 6, 4, 1] (which is result of [1, (3 + 1), (3 + 3), (1 + 3), 1] (passed)
// 6 returns [1, 5, 10, 10, 5, 1] (which is result of [1, (4 + 1), (6 + 4), (4 + 6), (1 + 4), 1] (passed)
// 0 returns undefined (passed)
// this function will just build the triangle, another function will print it out - keep out side effects
var pascalTriangle = function (numberOfLines) {
var result = [];
if (numberOfLines === 0) {
return undefined;
}
result.push([1]); // we need to add them as arrays
if (numberOfLines === 1) {
return result;
}
result.push([1, 1]);
if (numberOfLines === 2) {
return result;
}
var createNextRow = function (lastRowCreated) {
var newRowResult = [];
newRowResult.push(1) //add first element
for (var i = 1; i < lastRowCreated.length; i++) { //we are only creating the inner elements, shortened loop
newRowResult.push(lastRowCreated[i - 1] + lastRowCreated[i]);
}
newRowResult.push(1) //add last element
return newRowResult;
}
/// TESTING: createNextRow in console:::PASSED
for (var i = 3; i <= numberOfLines; i++) {
result.push(createNextRow(result[result.length - 1])); //pass in the last row we created
}
return result;
}
//////////TESTING: pascalTriangle in console:::PASSED///
//Items for future refactoring:
//We could probably rewrite this as a recursive algorithm to make it clearer, but this works
//As mentioned earlier, we would write a separate function to actually print this out - that way we
// can use the results in more parts of the program rather than just printing results to the console.
@AjayPoshak
Copy link

AjayPoshak commented Jun 6, 2017

I think that this can also be an alternative and shorter solution to PascalTriangle::

function pascalTriangle(lines){
    if(lines === 0){
        return undefined;
    } else {
        let mainArr = [] //Main Array, 2D array
        for(let i=0; i<lines; i++){
            let arr = [] // 1D array
            for(let j=0; j<=i; j++){
                if(j ==0 || j==i){
                    arr[j] = 1;
                } else {
                    arr[j] = mainArr[i-1][j] + mainArr[i-1][j-1]
                }
            }
            mainArr.push(arr); //inserting 1D array into 2D array
        }
        return mainArr;
    }
}

let output = pascalTriangle(6)
console.log(output);

@AjayPoshak
Copy link

By the way, thanks a lot for listing out the questions and their solutions.

@dothanhtung132
Copy link

let arr = Array(6).fill().map((_, i) => Array(i+1).fill(1));
for (let i = 2; i < arr.length; i++) {
  let currArr = arr[i];
  let prevArr = arr[i-1];
  for (let j = 1; j < currArr.length-1; j++) {
  	currArr[j] = prevArr[j] + prevArr[j-1];
  }
}
console.log(arr);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment