MockQuestions

Javascript Beginner Level Implementation Mock Interview

To help you prepare for your Javascript Beginner Level Implementation interview, here are 11 interview questions and answer examples.

Get More Information About Our Javascript Beginner Level Implementation Interview Questions

Question 1 of 11

Validate a square.

You are given four points in 2-D space. Return true if they form a square, false otherwise. The points are integer coordinates and are given in any order.

Valid square- A quadrilateral with all sides equal and both diagonal equal. Also, each interior angle is equal to 90°.

/* Example */

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Expected output- true

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Expected output- false

Solution:

For four points, we can find distances between them. There are a total of 6 distances possible. To be a valid square, 4 of them should be equal (the sides) and 2 should be equal (diagonals), and satisfies the following condition (Pythagoras theorem)-
diagonal2 = 2 * side^2

Instead of storing actual sides or diagonal, we are storing their square to avoid precision loss due to imperfect squares.

/**
 * @param {number[]} p1
 * @param {number[]} p2
 * @param {number[]} p3
 * @param {number[]} p4
 * @return {boolean}
 */
function isValidSquare(p1, p2, p3, p4) {
    const points = [p1, p2, p3, p4]
    const distances = []
 
    // find distances between all points
    for (let i = 0; i < 4; i++) {
        const p1 = points[i]
        for (let j = i + 1; j < 4; j++) {
            const p2 = points[j]
            const d = getDistanceSquared(p1, p2)
            distances.push(d)
        }
    }
    
    // make two partitions, one with equal to the first distance,
    // and one with not-equal to the first distance
    let a1 = distances.filter(a => a === distances[0])
    let a2 = distances.filter(a => a !== distances[0])
    
    // filter a2 such that it contains only equal distances
    a2 = a2.filter(a => a === a2[0])
    
    
    return areValidSides(a1, a2) || areValidSides(a2, a1)    
};
 
function areValidSides(diagonals, sides) {
    if(diagonals.length !== 2 || sides.length !== 4) return false
 
    // check if 2 * square(side) equal to square(diagonal)
    return 2 * sides[0] === diagonals[0]
}
 
function getDistanceSquared(p1, p2) {
    const diffXSquared = Math.pow(p1[0] - p2[0], 2)
    const diffYSquared = Math.pow(p1[1] - p2[1], 2)
 
    return diffXSquared + diffYSquared
}

Time complexity - O(1) // only four points
Space complexity - O(1)

Written by on May 4th, 2021

Next Question

11 Javascript Beginner Level Implementation Interview Questions & Answers

  • 1. Validate a square.

  • 2. Format a string by grouping characters found in the string into similar lengths.

  • 3. Determine if str1 is a subsequence of str2.

  • 4. Find the largest two numbers in an array without sorting them.

  • 5. Find unique vanishing numbers.

  • 6. How do you find two integers in the array such that their sum is 'target'?

  • 7. How do you determine if a string is valid or not?

  • 8. How can you find the third largest number in an array?

  • 9. Find if the array contains a unique number of occurrences for each value in the array or not.

  • 10. Create a running sum of a 1-D array.

  • 11. Find if the robot returns to its home after given instructions or not.