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

MockQuestions

Java Intermediate Level Arrays Mock Interview

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

Get More Information About Our Java Intermediate Level Arrays Interview Questions

Question 1 of 7

Can you implement a class SubRectangle?

This interview question concentrates on using Java 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 SubrectangleQueries {

    public SubrectangleQueries(int[][] rectangle) {
        
    }
    
    public void updateSubrectangle(int row1, int col1, int row2, 
int col2, int newValue) {
        
    }
    
    public int getValue(int row, int col) {
        
    }
}

/**
 * Your SubrectangleQueries object will be instantiated and called as such:
 * SubrectangleQueries obj = new SubrectangleQueries(rectangle);
 * obj.updateSubrectangle(row1,col1,row2,col2,newValue);
 * int param_2 = obj.getValue(row,col);
 */

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 SubrectangleQueries {

    private int rect[][];
    
    public SubrectangleQueries(int[][] rectangle) {
        rect = rectangle;
    }
    
    public void updateSubrectangle(int row1, int col1, int row2,
 int col2, int newValue) {
        for (int i = row1; i <= row2; i++) {
            for (int j = col1; j <= col2; j++) {
                rect[i][j] = newValue;
            }
        }
    }
    
    public int getValue(int row, int col) {
        return rect[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 update 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 SubrectangleQueries {

    private int rect[][];
    ArrayList<int[]> history;
    
    public SubrectangleQueries(int[][] rectangle) {
        rect = rectangle;
        history = new ArrayList<int[]>();
    }
    
    public void updateSubrectangle(int row1, int col1, int row2, 
int col2, int newValue) {
        history.add(new int[]{row1, col1, row2, col2, newValue});
    }
    
    public int getValue(int row, int col) {
        for (int i = history.size() - 1; i >= 0; i--) {
            int history[] = this.history.get(i);
            boolean isUpdated =
                history[0] <= row &&
                row <= history[2] && 
                history[1] <= col &&
                col <= history[3];
 
            if (isUpdated) {
                return history[4];
            }
        }
        
        return rect[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 June 27th, 2021

Next Question

7 Java Intermediate Level Arrays Interview Questions & Answers

Below is a list of our Java 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.