No results found for the specified position. 7 JavaScript Intermediate Level Arrays Interview Questions

MockQuestions

JavaScript Intermediate Level Arrays Mock Interview

To help you prepare for your JavaScript Intermediate Level Arrays interview, here are 7 interview questions and answer examples.

Get More Information About Our JavaScript Intermediate Level Arrays Interview Questions

Question 1 of 7

Can you implement a class SubRectangle?

This interview question concentrates on using JavaScript updates.

Your task is to implement a class SubRectangle. The constructor takes a 2-D matrix of size n X m and stores it. There are two methods in the class:- updateSubrectangle and getValue.
● The updateSubrectangle updates a portion of the store 2-D matrix with value newValue of the subrectangle whose top-left corner is (row1, col1) and bottom-right corner is (row2, col2).
● The method getValue returns the value at the provided location.

/*Example*/

passed matrix to the constructor-
[[1, 2, 1],
 [4, 3, 4],
 [3, 2, 1],
 [1, 1, 1]]

the calls made:-
1.	getValue(0, 2) // returns 1
2.	updateSubRectangle(0, 0, 3, 2, 5) // the array becomes:- 
[[5, 5, 5],
 [5, 5, 5],
 [5, 5, 5],
 [5, 5, 5]]
3.	getValue(0, 2) // returns 5
4.	getValue(3, 1) // returns 5
5.	updateSubRectangle(3, 0, 3, 2, 10) // the array becomes:- 
[[5, 5, 5],
 [5, 5, 5],
 [5, 5, 5],
 [10, 10, 10]]
6.	getValue(3, 1) // returns 10
7.	getValue(0, 2) // returns 5
class SubRectangle {
    /**
     * @param {number[][]} rectangle
     */
    constructor(rectangle) {
        // TODO
    }

    /** 
     * @param {number} row1 
     * @param {number} col1 
     * @param {number} row2 
     * @param {number} col2 
     * @param {number} newValue
     * @return {void}
     */
    updateSubRectangle(row1, col1, row2, col2, newValue) {
        // TODO
    }

    /** 
     * @param {number} row 
     * @param {number} col
     * @return {number}
     */
    getValue(row, col) {
        // TODO
    }
}

Solution:

Approach I (actual update):

After each update call, we will update the given matrix within the passed coordinates of the rectangle. While querying, we directly return the value at passed coordinates.

class SubRectangle {
    /**
     * @param {number[][]} rectangle
     */
    constructor(rectangle) {
        this.rectangle = rectangle
    }

    /** 
     * @param {number} row1 
     * @param {number} col1 
     * @param {number} row2 
     * @param {number} col2 
     * @param {number} newValue
     * @return {void}
     */
    updateSubRectangle(row1, col1, row2, col2, newValue) {
        for (let i = row1; i <= row2; i++) {
            for (let j = col1; j <= col2; j++) {
                this.rectangle[i][j] = newValue
            }
        }
    }

    /** 
     * @param {number} row 
     * @param {number} col
     * @return {number}
     */
    getValue(row, col) {
        return this.rectangle[row][col]
    }
}

Time complexity of updateSubRectangle- O(m * n)
Space complexity of updateSubRectangle- O(1)
Time complexity of getValue O(1)
Space complexity of getValue O(1)

Approach II (lazy update):

In this approach, we will not the rectangle matrix. Instead we will store the update history:- all the arguments passed to updateSubRectangle will be pushed to this.history as a new array. So the value of this.history after the two calls updateSubRectangle(0, 0, 3, 2, 5) and updateSubRectangle(3, 0, 3, 2, 10) of the example will be:-
this.history = [
[0, 0, 3, 2, 5],
[3, 0, 3, 2, 10]
]
Then during getValue, we will check the most recent update to the value at that coordinates. We will check from the last update to the earliest update i.e. loop in reverse direction on the history array. As soon as we find an update, we will return its value. But, if we don’t find any update in the history array, i.e. it is not updated by any operation, we return the original value this.rectangle[row][col].

class SubRectangle {
    /**
     * @param {number[][]} rectangle
     */
    constructor(rectangle) {
        this.rectangle = rectangle
        this.history = new Array()
    }

    /** 
     * @param {number} row1 
     * @param {number} col1 
     * @param {number} row2 
     * @param {number} col2 
     * @param {number} newValue
     * @return {void}
     */
    updateSubRectangle(row1, col1, row2, col2, newValue) {
        this.history.push(Array.from(arguments))
    }

    /** 
     * @param {number} row 
     * @param {number} col
     * @return {number}
     */
    getValue(row, col) {
        for (let i = this.history.length - 1; i >= 0; i--) {
            const history = this.history[i]
            const isUpdated =
                history[0] <= row &&
                row <= history[2] && 
                history[1] <= col &&
                col <= history[3]
 
            if (isUpdated) {
                return history[4]
            }
        }
 
        return this.rectangle[row][col]
    }
}

Time complexity of updateSubRectangle- O(1)
Space complexity of updateSubRectangle- O(1)
Time complexity of getValue- O(size of history)
Space complexity of getValue- O(1)

Written by on May 22nd, 2021

Next Question

7 JavaScript Intermediate Level Arrays Interview Questions & Answers

Below is a list of our JavaScript Intermediate Level Arrays interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Can you implement a class SubRectangle?

  • 2. Using the minimum number of operations, move all balls to the ith box.

  • 3. Find the winner of an array game.

  • 4. Sort the arrays in colors.

  • 5. What is the max height increase to keep the city skyline?

  • 6. Return the number of battleships on the board.

  • 7. Return a list of coordinates in a spiral matrix.