Solve These Three Simple Problems Before Your Next Interview

Geno Tech
4 min readMar 21, 2021

Ready for your next tech interview today #3

Problem-solving is a very important skill that you can just expose within a short time period. But, You need to follow it day by day and keep in touch with similar problems which definitely ask on a technical interview. Every technical interviewer nowadays prepared to ask one or many problem-solving questions to measure the ability of your skill with problem-solving. However, people are clever to solve these problems. but, they need to practise these problems in day to day life. This series of stories make it easy at your next interview. This is also very useful in every position for you as a Software Engineer/Developer, Senior Software Engineer, Tech Lead, Full-Stack Developer, Data Scientist etc.

Let’s go to the first problem. Every Problem has a specific order to follow in this article. First, describe the question, then answer in Java, finally the explanation. You can ask any question regarding these problems below.

Question #1: Difference of a Diagonal

  • Problem

Given a square matrix(2D Array), calculate the absolute difference between the sums of its diagonals.

For example, the square matrix arr is shown below:

1 2 5
7 3 6
4 8 9

The sum of the left-to-right diagonal; 1 + 3+ 9 = 13. The sum of the right to left diagonal; 5+3+4=12. Their absolute difference is |13–12|= 1. The answer is 1.

  • Solution (In Java)
public static int diagonalDifference(List<List<Integer>> arr) {

int sqrt = arr.size();

int i = 0;
int left = 0;
int right = 0;
while (i < arr.size()) {

left+=arr.get(i).get(i);
right+=arr.get(i).get(sqrt-(i+1));

i++;
}

return Math.abs(left-right);
}
  • Explanation

The method gets the 2 dimensional Array List as the input parameter. Here I initialized four variables. They are,

sqrt(int) - size of the array
i(int) - count 0 to the size of the array
left(int) - Count the sum of left to right diagonal
right(int) - Count the sum of right to left diagonal

Then we count the two sum values of two diagonals using a while loop. Here you need to add the correct value to each sum values carefully. Finally, return the absolute value of the difference between left and right.

Question #2: Plus Minus

  • Problem

Given an array of integers, you need to calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.

  • Solution(In Java)
public class Solution {

// Complete the plusMinus function below.
static void plusMinus(int[] arr) {

int positive = 0;
int negative = 0;
int zero = 0;

for(int i=0;i<arr.length;i++){
if(arr[i]<0){
negative++;
}else if(arr[i]==0){
zero++;
}else{
positive++;
}
}

System.out.println((float)positive/arr.length);
System.out.println((float)negative/arr.length);
System.out.println((float)zero/arr.length);

}
  • Explanation

In this solution method, I have to get three variables to count the number of positive, negative and zero value. Then implemented an array and go through each element of the input array. Inside the for loop, Checked the array element is a positive, negative or zero value. Then count the appropriate variable by one. Finally, print the results.

Question #3: A Very Big Sum

  • Problem

In this problem, you are required to calculate the sum of the elements in an array, Be careful and keeping in mind that some of those integers may be quite large.

  • Sample input
5
1000000005 1000000010 1000000030 1000000003 1000000001
  • Sample output
5000000049
  • Solution(In Java)
// Complete the aVeryBigSum function below.
static long aVeryBigSum(long[] ar) {
long sum = 0L;

for(int i=0;i<ar.length;i++){
sum+=ar[i];
}

return sum;

}
  • Explanation

This is not actually a very difficult problem. You can find out different ways to calculate the sum of an array. But here you need to aware of variable types to store different values in every programming language. Here the sum will be a very big value. Therefore, we need to know about Data types in the language you prefer. when we store numeric values, there have six ways as follows.

byte
size = 8 bits
range = Stores whole numbers from -128 to 127

short
size = 16 bits
range = Stores whole numbers from -32,768 to 32,767

int
size = 32 bits
range = Stores whole numbers from -128 to 127

long
size = 64 bits
range = Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float
size = 32 bits
range = Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double
size = 64 bits
range = Stores fractional numbers. Sufficient for storing 15 decimal digits

Conclusion

This story solved another three interview questions you will face in your next interview. So I hope, you have enjoyed it. Please feel free to ask any question you will face in the response section below. Also, ask any other related questions you have.
Happy Coding !!!!
Found this post useful? Kindly tap the 👏 button below! :)

--

--

Geno Tech

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.