Sunday, May 5, 2024
HomeJavaAmazon Interview Query - How one can Rely Detrimental Numbers in a...

Amazon Interview Query – How one can Rely Detrimental Numbers in a Sorted Matrix? [Solved]


Good day guys, earlier I shared an inventory of Google Coding Interview questions, and as we speak, I’m going to share with you an fascinating coding downside
which was requested on Amazon,
tips on how to rely complete unfavourable numbers in a given matrix the place rows and
columns are sorted in rising order
. Once more, I discovered this coding downside whereas browsing on the web, It
wasn’t truly requested to me or my reader, so I can not vouch that it is
truly an
Amazon Interview query. Although, I actually anticipate it to be as a result of it is an fascinating downside and
the optimum resolution will not be really easy however with the web, you by no means
know.

Anyway, as an alternative of specializing in whether or not this coding downside was requested in
Amazon or not, let’s give attention to how we are able to remedy this downside, however earlier than
that, let’s revise the issue assertion and see a few examples.

Drawback – Write a program in your favourite programming language (Java,
Python,
Ruby,
C++,
JavaScript, or no matter) to rely a complete variety of unfavourable integers in a matrix
the place rows and columns are sorted in rising order.

Instance – If the given matrix is beneath then the whole variety of unfavourable
numbers is 3 as there are three unfavourable numbers 2 within the first row (-5 and
-2) And one within the second row (-2). 

[-5, -2, 3]

[-2, 3, 4]

[3, 4, 5]

How one can rely unfavourable numbers in a sorted matrix?

Earlier than we take into consideration the answer, let’s first take into consideration all of the
data which is on the market to you. For instance, matrix is sorted, it
incorporates numbers, how large may be that its one other query and it incorporates
each constructive and unfavourable numbers. It will aid you to discover a resolution
shortly.   

Effectively, there are a few methods to unravel this downside and we’ll them.
First, let’s begin with the brute first resolution as we do all the time. 

Resolution 1: Brute pressure or Naive resolution

The simplest method to remedy this downside goes by means of every ingredient and
counting whether or not the quantity is unfavourable or not, however meaning it might take
O(N*M) occasions the place N is plenty of rows and M is plenty of columns. If
N=M then O(n^2) will not be an optimum
resolution. 

Can we do that higher?

How to count negative numbers in a sorted matrix? [Solved] Example

Resolution 2: Non-compulsory Resolution

Within the first resolution, we’re not utilizing the truth that rows and columns of
the matrix are in sorted order. Should you think about that reality, we might include
a greater resolution. Let’s assume we begin with the final column within the first
rows and go backward to search out the final unfavourable quantity in that listing.

In our instance, it might be the second ingredient within the first rows, which
means there will likely be solely two unfavourable numbers within the first row. 

Now, if transfer to the identical index within the subsequent row, and do that once more, we’ll
attain -2 and that’s the first ingredient within the second row, so in complete now we
have 2 + 1 = 3 unfavourable numbers. Within the final rows, if we begin with the identical
index, it is a constructive quantity so we’re finished already. Reply could be 3
unfavourable numbers.

That is
attainable as a result of every row and column are sorted in rising order.
This implies something earlier -2 within the row will likely be unfavourable, and something
beneath will even be higher than it. 

That is why after we began with the second row, we did not begin with the
final ingredient of the second row once more, as a result of it can’t be unfavourable, since
columns are additionally sorted on rising order and the primary ingredient within the
final column was constructive.

The time complexity of this downside is
O(N+M) the place N is the variety of
rows and M is the variety of columns, which suggests it is a lot better than the
earlier resolution. 

By the way in which, should you wrestle to calculate the time and area complexity of
your resolution or any algorithm or if you wish to study extra about then you definately
also can take a look at these
finest knowledge construction programs

to begin with. 

Java Program to rely unfavourable numbers in a sorted matrix

Now that you simply perceive the logic, let’s write down each brute pressure and
this optimum resolution in your favourite programming languages like Java or
Python. Since I like Java, I’m going to put in writing the answer in
Java, it’s possible you’ll do it on
Python,
C++,
Ruby, or
JavaScript
no matter you want.
import static org.junit.Assert.assertEquals;

import org.junit.Earlier than;
import org.junit.Ignore;
import org.junit.Take a look at;
/*
* Drawback - discover the primary recurring character in given String
* Examples
* if given String is "ABCDA" return "A"
* if given String is "ABCDAB" return "A"
* if given String is "ABBCDA" return "B"
* if given String is "ABCD" return null
*/
public class GoogleTest {
AmazonQuestion _instance;

@Earlier than
public void init(){
_instance = new AmazonQuestion();
}

@Take a look at
public void testBrutForceSolution(){

assertEquals(0, _instance.bruteForce(new int[][]{}, 0, 0));
assertEquals(1, _instance.bruteForce(new int[][]{{-1, 0}, {0, 1}}, 2, 2));
assertEquals(3, _instance.bruteForce(
new int[][]{{-2, -1, 0}, {-1, 0, 1}}, 2, 3));
assertEquals(5, _instance.bruteForce(
new int[][]{{-3, -2, 1}, {-2, -1, 0}, {-1, 0, 1}}, 3, 3));
assertEquals(0, _instance.bruteForce(
new int[][]{{7, 8, 9}, {8, 9, 10}, {9, 10, 11}}, 3, 3));
assertEquals(9, _instance.bruteForce(
new int[][]{{-6, -5, -4}, {-5, -4, -3}, {-4, -3, -2}}, 3, 3));
}

@Take a look at
public void testOptimalSolution(){
assertEquals(0, _instance.optimalSolution(new int[][]{}, 0, 0));
assertEquals(1, _instance.optimalSolution(new int[][]{{-1, 0}, {0, 1}}, 2, 2));
assertEquals(3, _instance.optimalSolution(
new int[][]{{-2, -1, 0}, {-1, 0, 1}}, 2, 3));
assertEquals(5, _instance.optimalSolution(
new int[][]{{-3, -2, 1}, {-2, -1, 0}, {-1, 0, 1}}, 3, 3));
assertEquals(0, _instance.optimalSolution(
new int[][]{{7, 8, 9}, {8, 9, 10}, {9, 10, 11}}, 3, 3));
assertEquals(9, _instance.optimalSolution(
new int[][]{{-6, -5, -4}, {-5, -4, -3}, {-4, -3, -2}}, 3, 3));
}


}


class AmazonQuestion {

public int bruteForce(int[][] matrix, int rows, int columns){
int complete = 0;

for(int i=0; i= 0){
if(matrix[i][j] < 0){
complete = complete + j + 1;
i++;
}else{
j--;
}
}

return complete;
}


}

Output: All assessments handed

Whenever you run this program as a
JUnit check, you’ll find that every one assessments are passing which suggests our resolution is
good. Should you look intently, you’ll find that I’ve two assessments, one for every
resolution, I imply one for brute pressure and one for the optimum resolution. 

Every check has 4 assertions or situations to test, that are an precise
check for logic applied in brute pressure and our optimum resolution, which
contains testing for an empty array, testing of the array with each constructive
and unfavourable numbers, testing for an array with solely constructive and unfavourable
numbers, and testing with an array of various size.

That is all about
tips on how to rely a complete variety of unfavourable integers in a given Matrix

the place each rows and columns are sorted in ascending order. We have now mentioned
two options, Brute pressure checks each ingredient to see if it is unfavourable or
not, whereas the optimum resolution solely checks nearly half of the weather. It
takes benefit of the truth that rows and columns of the matrix are already
sorted in ascending order.

Different Coding Issues from Interviews for Observe

If you’re curious about fixing extra String primarily based coding issues from
Google, Fb, Amazon, and different FAANG corporations then right here is the listing
of hottest string questions

  • How one can test if a string incorporates solely digits?  (resolution)
  • How one can rely the variety of vowels and consonants in a String? (resolution)
  • How one can rely the prevalence of a given character in String? (resolution)
  • 10 Algorithms Programs to Crack Programming Job Interviews (programs)
  • How one can reverse phrases in a sentence with out utilizing a library methodology? (resolution)
  • How one can test if String is Palindrome? (resolution)
  • How one can discover the primary recurring character from a given String in Java?
    (resolution)
  • How one can discover all permutations of String? (resolution)
  • How one can test if the given String is the rotation of one another? [solved]
  • How one can Print duplicate characters from String? (resolution)
  • How one can convert a numeric string to an int? (resolution)
  • How one can test if two Strings are anagrams of one another? (resolution)
  • How one can program to print the primary non-repeated character from String?
    (resolution)
  • How one can reverse String in Java utilizing Iteration and Recursion? (resolution)
  • How one can return the best occurred character in a String? (resolution)
  • How one can reverse a String in place in Java? (resolution)
  • 50+ Information Construction and Algorithms Interview Questions (questions)

Thanks for studying this text thus far. Should you like this
Amazon coding Interview downside and my resolution and rationalization
then please share it with your folks and colleagues. When you have any
questions or suggestions then please drop a observe.

P. S. – If you’re getting ready for an interview, take this
FAANG Interview preparation course
which might your an concept about what sort of questions comes on FAANG
interviews and tips on how to remedy them in a restricted time. There was a time when
discovering good sources was very onerous however now you can crack the FAANG
interview through the use of these sorts of sources. 
P.S.S. – Should you really feel your knowledge construction and algorithm abilities are
rusty and also you want a little bit of refresher then I counsel you are taking this free knowledge construction and algorithms course to shortly revise all vital ideas.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments