# These Problems Will Ask In Your Next Interview: Problem-Solving Questions and Answers

Ready for your next tech interview today #4

Problem-solving is a compulsory skill you have as a professional in the Technological field. It is testing during your interview process. If you succeed in this challenge, you have the extra potential to get your new job. Because most of the developers are only able to do traditional tasks, but they fail to solve problems. Another problem is the lack of resources to learn about these problems. Sometimes very expensive, Sometimes have no enough clarifications and answers. Also, you need to practise these questions day by day, that’s the way you have to develop this skill. There have other advantages also. 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 question. Every Problem has a specific order to follow as follows.

- Description of the question
- Our answer in
- Explanation.

We are ready to accept any question regarding these problems you have.

# Question #1: Birthday Cake Candles

You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

For example, the integer array, “*candles” is *shown below:

`[4, 4, 1, 3]`

The maximum height candles are units high. There are of them, so return 2.

- Solution (In Java)

For example, the square matrix *arr *is shown below:

public static int birthdayCakeCandles(List<Integer> candles) { int max = getMax(candles); int count = 0; for(int i=0;i<candles.size();i++){ if(candles.get(i) == max) count+=1; }return count;}

- Explanation

In every problem, It’s important to see the whole problem as tiny steps. That’s the easiest way to solve any problem. On this problem, first, we need to find the maximum of the List. Then need to find how many elements represent that value in the List. so In my solution, We can find the maximum of a List like follows.

`int max = getMax(candles);`

Then I have found the number of maximums represents in the List using a for a loop.

for(int i=0;i<candles.size();i++){ if(candles.get(i) == max) count+=1;}

# Question #2: Pairs of Socks

- Problem

There is a large pile of socks that must be paired by colour. Given an array of integers representing the colour of each sock, determine how many pairs of socks with matching colours there are.

- Solution(In Java)

`static int sockMerchant(int n, int[] ar) {`

int pairs = 0 ;

int count = 1;

boolean exists = false;

for(int i=0;i<(ar.length-1);i++){

for(int a=i-1;a>=0;a--){

if(ar[i] == ar[a]){

exists = true;

}

}

if(!exists){

for(int j=i+1;j<ar.length;j++){

if(ar[i] == ar[j]){

count++;

if(count % 2 == 0){

pairs++;

}

}

}

}

exists = false;

count = 1;

}

return pairs;

}

- Explanation

First of all, you want to divide the problem into pieces. That’s the way to solve the problem in an easy manner. Here we need a for loop to get numbers one by one. then we check the number has met before. If not, we can check how many numbers belongs to that particular number.

# Question #3: Creating a Magic Square

- Problem

We define a magic square to be an *n*n* matrix of distinct positive integers from to where the sum of every column, row, or diagonal of length is always equal to the same number that’s called the magic number.

You will be given a *3*3 *matrix ‘*arr’ *of integers in the inclusive range [1,9]. We can convert any digit *a *to any other digit *b *in the range [1,9] at cost of *|a-b|*. Given *arr*, convert it into a magic square at a *minimal *cost. Print this cost as the result.

- Solution(In Java)

`static int formingMagicSquare(int[][]s){`

int costs[]= new int[8];

int t[][]=

{

{4,9,2,3,5,7,8,1,6},

{4,3,8,9,5,1,2,7,6},

{2,9,4,7,5,3,6,1,8},

{2,7,6,9,5,1,4,3,8},

{8,1,6,3,5,7,4,9,2},

{8,3,4,1,5,9,6,7,2},

{6,7,2,1,5,9,8,3,4},

{6,1,8,7,5,3,2,9,4},

};

for(int i=0;i<8;i++)

{

costs[i]=Math.abs(t[i][0]-s[0][0])+Math.abs(t[i][1]-s[0][1])+Math.abs(t[i][2]-s[0][2]);

costs[i]=costs[i]+Math.abs(t[i][3]-s[1][0])+Math.abs(t[i][4]-s[1][1])+Math.abs(t[i][5]-s[1][2]);

costs[i]=costs[i]+Math.abs(t[i][6]-s[2][0])+Math.abs(t[i][7]-s[2][1])+Math.abs(t[i][8]-s[2][2]);

}

Arrays.sort(costs);

return costs[0];

}

- Explanation

It takes a bit of pen and paper to see that, the magic constant is equal to the 15. How you get it?. The sum of 1 to 9 is 45 and when we divide it by 3, we can get the magic sum is equal to 15.

Also, the middle value is always equal to 5. Then we can find that the numbers 2,4,6,8 are always positioning at corners and the numbers 1,3,5,7 are at the other four places respectively. This is the secret behind this story, you have to identify. Finally, you can found only 8 possible ways in which matrices can arrange.

Therefore in this solution, you need to create an array to store all possible solutions which we recognized previously, and we can calculate costs for all those solutions. Then the final result is the minimum cost.

The thinking part of this problem is very important to other matrix problems. Hopefully, this is enough explanation for this problem.

# Conclusion

This story solved another three interview questions you will face in your next interview. Hopefully, 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! :)