I got the online coding questions as part of interview process in ECS.
I was told next day that I have cleared the coding round & scored full marks. I wondered, as I knew I had not covered all the scenarios. Next round was telephonic round from UK. This discussion was around the behavioural side to understand your work & how did you handle the difficult team members in your team. It was of no use as per my understanding.
I have been through many interviews & believe me, no interviewer is ready to accept his/her creepy truth nor ready to hear honest words from you. All these people need/want to listen some cheesy/fancy/catchy technical words from you, which I believe that 90% candidates had not used in their career ever or not even listened before that interview. As many candidates mug-up some such words, definitions & have planned some story to relate with those words during their interviews.
So only one suggestion - If you urgently need some job, then screw the honesty & be ready with some fancy technical words, definitions & some story where you can use those words weave some of your work around that.
Now, for this I got 2 coding questions -
Question 1) You are given a number M such that some array will be having M balls each having value 1..M, i.e. ball 1 will
have value 1 & ball 2 will have value 2 and so on. Similarly you will be having bag which can hold the balls of
total value of M
Another array 'A' will be given having some numbers such that numbers <=M
'A' denotes the balls which shouldn't be considered. And you have to fill the bag with remaining balls such that
you can have maximum number of balls in the bag but its total value shouldn't exceed M. You need to return that
maximum numbers which this bag can hold.
Suggestion - Rather making the solution complex like shown below, if we see then it is like we need to find the maximum number of available balls(after removing the balls of A), whose total value is less than or equal to M. So remove the elements in A from the array having values 1 to M, then sort the remaining values in increasing order. Then initial values only will be giving the maximum numbers.
Solution :
import java.util.ArrayList;
import java.util.HashSet;
public class ECS1 {
static int results=0;
public static void main(String[] args) {
int[] array = {1,3,5};
int M = 10;
int N = 3;
System.out.println(MaxStones(M, N, array));//Ans 2
}
public static int MaxStones(int M, int N, int[] array)
{
HashSet<Integer> common = new HashSet<>();
HashSet<Integer> bag = new HashSet<>();
ArrayList<Integer> unique = new ArrayList<Integer>();
for(int i : array) {
common.add(i);
}
for(int i = 1; i <= M; i++) {
if(!common.contains(i)) {
unique.add(i);
}
}
int len = unique.size();
for(int i = 0; i < len; i++) {
int rem = M;
bag.clear();
if(unique.get(i) <= M) {
rem-=unique.get(i);
bag.add(unique.get(i));
find(i+1, len, rem, bag, unique);
if(bag.size() > results) {
results = bag.size();
}
} else
break;
}
return results;
}
public static void find(int N, int len, int rem, HashSet<Integer> bag, ArrayList<Integer> unique) {
for(int i = N; i < len; i++) {
if(rem > 0 && unique.get(i) <= rem) {
rem -= unique.get(i);
bag.add(unique.get(i));
find(i+1, len, rem, bag, unique);
} else
return;
}
}
}
Question 2) : You are given an array of numbers each denoting individual person type. Now we need to make the groups of
these persons such that each group has at least 2 persons having common type to avoid any conflicts in the
group. Provide the maximum number of groups can be created with this constraint, if no such group is possible
then return 0.
Solution :
import java.util.HashMap;
public class ECS2 {
static int result=0;
public static void main(String[] args) {
int[] N_arr = {2,1,3,1,2,2,5,6,2}; //Ans 3
// int[] N_arr = {1,2,3,4,5,6,1,2}; //Ans 2
System.out.println(CreateTeam(N_arr));
}
public static int CreateTeam(int[] N_arr)
{
HashMap<Integer, Integer> groups = new HashMap<>();
for(int i : N_arr) {
if(groups.get(i) == null) {
groups.put(i, 1);
} else {
int n = groups.get(i);
groups.put(i, n+1);
}
}
groups.forEach((a,b) -> {
result+=(b/2);
});
return result;
}
}
Below is the same logic and code but written in a bit different way-
I was told next day that I have cleared the coding round & scored full marks. I wondered, as I knew I had not covered all the scenarios. Next round was telephonic round from UK. This discussion was around the behavioural side to understand your work & how did you handle the difficult team members in your team. It was of no use as per my understanding.
I have been through many interviews & believe me, no interviewer is ready to accept his/her creepy truth nor ready to hear honest words from you. All these people need/want to listen some cheesy/fancy/catchy technical words from you, which I believe that 90% candidates had not used in their career ever or not even listened before that interview. As many candidates mug-up some such words, definitions & have planned some story to relate with those words during their interviews.
So only one suggestion - If you urgently need some job, then screw the honesty & be ready with some fancy technical words, definitions & some story where you can use those words weave some of your work around that.
Now, for this I got 2 coding questions -
Question 1) You are given a number M such that some array will be having M balls each having value 1..M, i.e. ball 1 will
have value 1 & ball 2 will have value 2 and so on. Similarly you will be having bag which can hold the balls of
total value of M
Another array 'A' will be given having some numbers such that numbers <=M
'A' denotes the balls which shouldn't be considered. And you have to fill the bag with remaining balls such that
you can have maximum number of balls in the bag but its total value shouldn't exceed M. You need to return that
maximum numbers which this bag can hold.
Suggestion - Rather making the solution complex like shown below, if we see then it is like we need to find the maximum number of available balls(after removing the balls of A), whose total value is less than or equal to M. So remove the elements in A from the array having values 1 to M, then sort the remaining values in increasing order. Then initial values only will be giving the maximum numbers.
Solution :
import java.util.ArrayList;
import java.util.HashSet;
public class ECS1 {
static int results=0;
public static void main(String[] args) {
int[] array = {1,3,5};
int M = 10;
int N = 3;
System.out.println(MaxStones(M, N, array));//Ans 2
}
public static int MaxStones(int M, int N, int[] array)
{
HashSet<Integer> common = new HashSet<>();
HashSet<Integer> bag = new HashSet<>();
ArrayList<Integer> unique = new ArrayList<Integer>();
for(int i : array) {
common.add(i);
}
for(int i = 1; i <= M; i++) {
if(!common.contains(i)) {
unique.add(i);
}
}
int len = unique.size();
for(int i = 0; i < len; i++) {
int rem = M;
bag.clear();
if(unique.get(i) <= M) {
rem-=unique.get(i);
bag.add(unique.get(i));
find(i+1, len, rem, bag, unique);
if(bag.size() > results) {
results = bag.size();
}
} else
break;
}
return results;
}
public static void find(int N, int len, int rem, HashSet<Integer> bag, ArrayList<Integer> unique) {
for(int i = N; i < len; i++) {
if(rem > 0 && unique.get(i) <= rem) {
rem -= unique.get(i);
bag.add(unique.get(i));
find(i+1, len, rem, bag, unique);
} else
return;
}
}
}
Question 2) : You are given an array of numbers each denoting individual person type. Now we need to make the groups of
these persons such that each group has at least 2 persons having common type to avoid any conflicts in the
group. Provide the maximum number of groups can be created with this constraint, if no such group is possible
then return 0.
Solution :
import java.util.HashMap;
public class ECS2 {
static int result=0;
public static void main(String[] args) {
int[] N_arr = {2,1,3,1,2,2,5,6,2}; //Ans 3
// int[] N_arr = {1,2,3,4,5,6,1,2}; //Ans 2
System.out.println(CreateTeam(N_arr));
}
public static int CreateTeam(int[] N_arr)
{
HashMap<Integer, Integer> groups = new HashMap<>();
for(int i : N_arr) {
if(groups.get(i) == null) {
groups.put(i, 1);
} else {
int n = groups.get(i);
groups.put(i, n+1);
}
}
groups.forEach((a,b) -> {
result+=(b/2);
});
return result;
}
}
Below is the same logic and code but written in a bit different way-