Round 1:
Question 1:
How to handle long running task asynchrnously and get it completed. How will you design such system for this purpose. Tell me the perfect approach.
//IFor example: Install and configure VMWare service. Here what if my one container fails and other container want to pick up the task from the point where it was left incomplete.
Solution:
I suggested her to divide the task as below:
1) Divide
2) Fetch api __package__
3) Install each package /execution
4) Verification /test
5) Deployment on client machine
6) Vulnerability chk
Main agreed Solution:
Save request and response in DB
Note: server cache solution will fail as we are talking about multiple containers - which run on different client machines not servers.Question 2:
1. public class Singlton {
2.
3. public static final singleton = null;
4.
5. Singlton() {}
6.
7. public static Singlton getInstance() {
8.
9. if(sington == null) {
10. //object level locking
11. synchronized(this) {
12. if(singlton == null) {
13. singleton = new Singlton();
14. }
15. }
16. }
17. else singlton;
18. }
19. }
- Difference between class level lock and object level lock
- Line 11, what does synchronized block does here
- what if multiple thread reaches at line 9 and try to access the same, what will happen to both the threads
(Ans: Add null check at line 12)
- What is lazy initialization
- what if you write sunchronized keyword at getInstance method then having synchronized block (And: Reduces performance as lock is done at method level)Q3: Implement your own hashmap
//Functions: Buckets - Entry<Key, value>
//get
//put
Answer:
public class Map<K, V> {
private Entry<k, V>[] buckets;
public static final int CAPACITY = 16;
Map(Entry<k,v>[] buckets) {
this.buckets = buckets;
}
Map() {
buckets = new Entry[CAPACITY];
}
public get() {
}
public put(K key, V value) {
Entry<K, V> e = new Entry<K, V>()key, value)
if(key == null) {
bucket[0] == obj;
}
//calculate hashcode
//buckNum = hashcode % capacity
//bucket[] = Entry;
int bucketNum = getHash() % CAPACITY;
if(bucket[bucketNum] == null) {
bucket[bucketNum] = e;
} else if( bucket[bucketNum] != null) {
if e.key.euqlas(buckets[bucketNum].getKey()) {
bucket[bucketNum] = e;
} else {
// Create LinkedList
// create Node
}
}
}
getHash(K key) {
//Object class hashcode()
return hashCode();
}
}Questions on this map implementation:
Round 2:
Programing Question 1:
Write a program to Return number of words in a sentense.
Criteria:
Programing Question 2:
Write a program to shift all 0's to left and 1's to right.
Input: Arr = [0,1,0,0,1,1,0]
Output: [0,0,0,1,1,1]
public class ShiftAllZeroLeft {
public static void main(String[] args) {
int[] arr = {1,1,1,0,0,1,0,0,1};
int[] outputArr = shiftElem(arr);
for (int i: outputArr) {
System.out.println(i);
}
}
static int[] shiftElem(int[] arr) {
int count=0;
for (int i: arr) {
if(i != 1) {
arr[count++] = i;
}
}
System.out.print("count" + count);
for(int i= count; i<arr.length; i++) {
arr[i] = 1;
}
return arr;
}
}**Programing Questions 3: **
Find number of missing duplicate element :
Input: a1 = {1,2,3,4};
a2 = {2,3,4,8,10};
Output: 3
import java.util.*;
import java.lang.*;
public class Assign {
public static int findMissingDuplicate(int[] a1, int[] a2) {
int count =0 ;
Set<Integer> set = new HashSet<>();
if( a1 == null || a2 == null) {
return 0;
} else {
for (int i : a1) {
//System.out.println("i"+i+" "+set.add(i));
if(!set.contains(i))
set.add(i);
else
set.remove(i);
}
for (int i : a2) {
//System.out.println("i"+i+" "+set.add(i));
if(!set.contains(i))
set.add(i);
else
set.remove(i);
}
count = set.size();
}
return count;
}
public static void main(String[] args) {
int[] a1 = {1,2,3,4};
int[] a2 = {2,3,4,8,10};
System.out.println(findMissing(a1, a2));
}
}Round 3:
Question 1:
Given Integer Array in sorted order (asc) => {1, 3, 4, 5, 6, 8, 9} =>
Ask is to find an integer => find(5) => 3 ,
find (2) => -1
public class Find {
public static int findElem(int[] arr, int elem) {
int s = 0, e = (arr.length-1);
while(s<=e) {
int mid = s+(e - s)/2;
if(elem == arr[mid]){
return mid;
}
if(elem <= arr[mid]) {
//left sub array
if (elem >= arr[s] && elem < arr[mid]) {
e = mid-1;
} else
s = mid +1;
} else{
//Right subarray
if(elem > arr[mid] && elem <= arr[e]) {
s = mid +1;
} else
e = mid -1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 3, 4, 5, 6, 8, 9};
System.out.println(findElem(arr, 2));Question 2:
arr1 = {2, 5, 8, 0, 0, 0, 0}, arr2 = {1, 3, 4, 7} , sorted (asc) , merge 2 array => return/merged array (sorted array having all the elements in sorted order)
public static int[] mergeArray(int[] a1, int[] a2) {
if(a1 == null)
return a2;
if (a2 == null)
return a1;
int i = a1.length-1;
int j = a2.length-1;
int k = (i+j) - 1;
int[] a3 = new int[i+j];
while(j>-1) {
if(i<0) {
a3[k--] = a2[j--];
} else {
if(a1[i] > a2[j]) {
a3[k--] = a1[i--];
} else
a3[k--] = a2[j--];
}
}
return a3;
}
public static void main(String[] args) {
int[] a1 = {2, 5, 8};
int[] a2 = {1, 3, 4, 7};
int[] a3 = mergeArray(a1, a2);
for (int i : a3) {
System.out.println(i);
}
Question 3:
Write a program to compare two objects. For example user. How can I have users sorted by name. which is the best data structure to be used. Implement all methods.
Solutions:
Either use comparable method or override hashCode and equals methods
Implementation:
```
public static void main() {
/*TreeSet<User> tm= new TreeSet<User>(new MyCompare());
tm.add(new User(1, "Ankita"));
tm.add(new User(2, "Sushil")); */
}
/*class MyCompare implements Comparator<User>{
public int compare(User u1, User u2) {
return u1.getUserName().compareTo(u2.getUserName());
}
}*/
}
class User {
int id;
String userName;
}
/*
@Override
int hashCode(Object o) {
if (u == null)
return 0;
else {
int primt = 31;
int uniq = u.userName + u.id;
int hash = prime + uniq;
//algo to unique hashing value cal
return hash;
}
}
@Override
boolean equals(Object o) {
if (o == null)
return false;
else {
if (getClass() == o.getClass())
return true;
}
}*/Round 4:
I was asked to write pseudo code for the below programming question. Rest all were project specific question which were asked by other interviewers as well, as mentioned below :
Programing 1:
Write a program to find the node at which the intersection of two singly linked lists exist
https://leetcode.com/problems/intersection-of-two-linked-lists/
Round 5: With Senior manager
Round 6:
-Introduction as usual
Design an api to whitelist IP address tenant specific. IP address should be configurable(add/update/select) from admin screen.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
@Component
public class AuthenticationProvider implements AuthenticationProvider {
Set<String> authIpList = new HashSet<String>();
public AuthenticationProvider() {
authIpList.add("10.35.12.23");
authIpList.add("10.35.12.25");
}
public Authentication authentic(Authentication auth) {
List<String> tenants = Env.get(kea.multi-tenancy);
http.authorize()
.antMatcher("/mg").hasIpAddr();
//contains code to whitelist IP
}
}
@RestController
@RequestMapping("\api\Security")
public class IpController {
private final IpService ipService;
@PostRequest("\ip\{ip_list}\{tenant}")
public ResponseEntity<String> addIp(PathVariable(value="ip_list") List<String> ipList, @PathVariable(value = "userName") String tenant) {
Set<String> set = new HashSet<>();
for (String ip : ipList) {
set.add(ip);
}
//Store in DB
String resCode = ipService.addIpList(set);
}
@PostRequest("\ip\{ip_list}\{tenant}")
public ResponseEntity<String> updateIp(PathVariable(value="ip_list") List<String> ipList, @PathVariable(value = "userName") String tenant) {
//Store in DB
String resCode = ipService.addIpList(set);
}
}
@Service
public class IpService extends RestService{
public String addIpList(Set<String ipList> ipList) {
//call repo to add
}
public String updateIp(List<String> ip) {
//call repo to update
}
public List<String> fetchAll(String tenantId) {
//call fetch
}
}
@Repository
public class IpRepo implement JPARepository{
List<String> selectList(string tenantId);
}
@Entity
public class IpAddr {
@IDProperty
@GeneratedValue()
int id;
@Column("ipAddr")
String ipAddr;
@Column("tenantid")
int tenantId;
/*Create table IpAdd {
//int id;
//varchar2 ipAddr;
//int tenantId
}
Database:
1 10.2.32.45 MG
*/
}
//Configuration file for multi tenancy
application.yml {
env: dev, stage, prod
kea:
multi-tenancy: axa, mg, aviva, jp, ss
}
}