Skip to content

Instantly share code, notes, and snippets.

@CastonPursuit
Last active February 13, 2024 18:01
Show Gist options
  • Save CastonPursuit/831e2f1c39ae80234592a7aea6ca8a8c to your computer and use it in GitHub Desktop.
Save CastonPursuit/831e2f1c39ae80234592a7aea6ca8a8c to your computer and use it in GitHub Desktop.

Peer Mock Interview

Keep In Mind
  • Allocate 25 minutes for each interview and 5 minutes to fill out the Peer Mock Interview Form after the 25 mins for the coding part. (30 mins total)
  • You can switch roles too but you can also only be an interviewer or interviewee.
  • Never give the solution but you can give helpful guidance if the candidate is veering away or has improper syntax.
  • If you don't submit a google form, the interviewee will not get credited for the interview. Submit it as soon as you close the interview.

Pre/Post-Interview Checklist

Make sure you have completed all of the following tasks before starting an interview:

  • You have the Peer Mock Interview Form open.
    • Confirm that your interviewee has not been interview with the problem you are presenting.
      • If they have, give them a problem they've never seen.
      • If they have seen every problem, ask them for the most recent and give them a different one from that
      • (it's okay if they have seen the problem more than once as long as they have been asked all the problems from this A document.)
    • Create a new repl.
    • Copy and paste the interview question from one of the questions below into the repl.it (please avoid pasting the solution, just the question).
    • Have the interviewee fork it.
    • Once they have forked it, have them invite you and to SHARE THEIR SCREEN.

      You will paste their repl.it URL link in the Peer Mock Interview Form.

    • Once you are done with the interview, take 5 minutes to fill out the Peer Mock Interview Form while the interviewee is present

      Be sure to capture the exact name of the interview question, which can be found below, i.e. _Interview Question: ____


Problems (Arrays/Loops)
Problem 1: Sum of Array

Problem Description:

/* Write a JavaScript function to calculate the sum of values in an array. */

///Example:
console.log(arraySum([1, 2, 3, 4])); // Output: 10

///Possible Solution
function arraySum(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum;
}
Problem 2: Max Value in Array

Problem Description:

 /* Create a function that finds and returns the highest number in an array. */

///Example:
console.log(maxValue([1, 5, 3, 9, 2])); // Output: 9

///Possible Solution
function maxValue(arr) {
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}
Problem 3: Find Element

Problem Description:

/* Write a function that checks whether an element exists in an array and returns its index if it exists, otherwise returns -1. */

///Example:
console.log(findElement([10, 20, 30, 40, 50], 30)); // Output: 2
console.log(findElement([10, 20, 30, 40, 50], 60)); // Output: -1
  
///Possible Solution
function findElement(arr, element) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === element) {
      return i;
    }
  }
  return -1;
}
Problem 4: Array Reverse

Problem Description:

/* Implement a function to reverse an array without using the built-in reverse method. */

///Example:
console.log(arrayReverse([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]
  
///Possible Solution
function arrayReverse(arr) {
  let reversedArray = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    reversedArray.push(arr[i]);
  }
  return reversedArray;
}
Problem 5: Count Occurrences

Problem Description:

/* Write a function that counts the number of times a specific value occurs in an array. */

///Example:
console.log(countOccurrences([1, 2, 2, 3, 4, 4, 4], 4)); // Output: 3
  
///Possible Solution
function countOccurrences(arr, value) {
  let count = 0;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === value) {
      count++;
    }
  }
  return count;
}
Problem 6: Filter Even Numbers

Problem Description:

/* Create a function that takes an array of numbers as input and returns a new array containing only the even numbers. */

///Example:
console.log(filterEvenNumbers([1, 2, 3, 4, 5, 6])); // Output: [2, 4, 6]
  
///Possible Solution
function filterEvenNumbers(arr) {
  let evenNumbers = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] % 2 === 0) {
      evenNumbers.push(arr[i]);
    }
  }
  return evenNumbers;
}
Problem 7: Remove Duplicates

Problem Description:

/* Implement a function to remove duplicate values from an array. The function should return a new array with only unique elements. */

///Example:
console.log(removeDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [1, 2, 3, 4, 5]
  
///Possible Solution
function removeDuplicates(arr) {
  let uniqueArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (!uniqueArr.includes(arr[i])) {
      uniqueArr.push(arr[i]);
    }
  }
  return uniqueArr;
}

                                 
Problem 8: Find the Second Largest Number

Problem Description:

/* Write a JavaScript function that returns the second largest number in an array. */

///Example:
console.log(findSecondLargest([1, 3, 4, 5, 0, 2])); // Output: 4
  
///Possible Solution
function findSecondLargest(arr) {
  let firstMax = Number.MIN_SAFE_INTEGER, secondMax = Number.MIN_SAFE_INTEGER;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > firstMax) {
      secondMax = firstMax;
      firstMax = arr[i];
    } else if (arr[i] > secondMax && arr[i] < firstMax) {
      secondMax = arr[i];
    }
  }
  return secondMax;
}
Problem 9: Rotate Array

Problem Description:

/* Create a function that rotates the elements of an array to the right by a given number of steps. 
For example, with steps = 1, [1, 2, 3] becomes [3, 1, 2]. */

///Example:
console.log(rotateArray([1, 2, 3, 4, 5], 2)); // Output: [4, 5, 1, 2, 3]
  
///Possible Solution
function rotateArray(arr, steps) {
  let result = new Array(arr.length);
  for (let i = 0; i < arr.length; i++) {
    result[(i + steps) % arr.length] = arr[i];
  }
  return result;
}
Problem 10: Merge Two Arrays

Problem Description:

/* Write a function that merges two arrays into one single array. 
For example, merging [1, 2, 3] and [4, 5, 6] should return [1, 2, 3, 4, 5, 6]. */

///Example:
console.log(mergeArrays([1, 2, 3], [4, 5, 6])); // Output: [1, 2, 3, 4, 5, 6]
  
///Possible Solution
function mergeArrays(arr1, arr2) {
  let mergedArray = [];
  for (let i = 0; i < arr1.length; i++) {
    mergedArray.push(arr1[i]);
  }
  for (let j = 0; j < arr2.length; j++) {
    mergedArray.push(arr2[j]);
  }
  return mergedArray;
}

Problems (Objects/Loops)
Problem 1: Dynamic Object Update

Problem Description:

Given an object personDetails that includes the person's name, age, and email, write a function updatePersonDetails that iterates over each property of the object. If the property key is age, increment it by 1. The function should add a new property updated set to true indicating that the object has been updated. The function should then return the updated object.

The personDetails object is as follows:

const personDetails = {
  name: "Jane Doe",
  age: 34,
  email: "[email protected]"
};

Solution:

function updatePersonDetails(person) {
  for (let key in person) {
    if (key === 'age') {
      person[key] += 1;
    }
  }
  person.updated = true;

  return person;
}

Example Usage:

console.log(updatePersonDetails(personDetails));

Expected Output for the example usage:

{
  name: "Jane Doe",
  age: 35, // Age after increment
  email: "[email protected]",
  updated: true // New property indicating the object was updated
}
Problem 2: Validate and Clean Contact List

Problem Description:

Given an object contactList representing a collection of contacts, write a function validateAndCleanContacts that iterates over each contact in the list. The function should check if each contact has valid email and phoneNumber fields (for simplicity, assume any non-empty string is valid). If a contact lacks a valid email or phone number, remove that contact from the list. The function should return the cleaned contactList.

Initial contactList Object:

const contactList = {
  "John Doe": {
    email: "[email protected]",
    phoneNumber: "123-456-7890"
  },
  "Jane Smith": {
    email: "", // Invalid email
    phoneNumber: "098-765-4321"
  },
  "Invalid Contact": {
    email: "",
    phoneNumber: ""
  }
};

Possible Solution with Loop:

function validateAndCleanContacts(contactList) {
  for (let name in contactList) {
    const contact = contactList[name];
    if (!contact.email || !contact.phoneNumber) {
      delete contactList[name];
    }
  }
  return contactList;
}

Example Usage:

console.log(validateAndCleanContacts(contactList));

Expected Output:

{
  "John Doe": {
    email: "[email protected]",
    phoneNumber: "123-456-7890"
  }
  // "Jane Smith" and "Invalid Contact" removed due to invalid info
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment