Round 1 :
- Tell me about your self
- What are the thing you need to take care while designing microservices - loose coupling, single responsibility
- Why do you think we need microservice
- Managing monolithic service is difficult, what challenge do you think can come if we opt for microservices
- What is single responsibility
- Benifits of gitlab over jenkins pipeline. as jenkins as many new features deployed recently
- How can you make microservices to interact with each other (Ans: REST calls)
- How to achieve asynchronous behaviour - eg: 2 users trying to book ticket in cinema and request at
- the same time, how will you manage(Dont use synchronized). i told them another approach called SQS.
- He asked except SQS how can you acheive it across multiple REST? (Mostly Saga is the answer)
- How to handle multi microservice based scenario with multiple functionaltiy in on request - on
- booking.com if user select flight and hotel at the same time, what is flight booking is
- successful and hotel fails, user multiple microservice and how will you handle this
- behavior (saga)
Ans: Flight microservice 1 is SU, DB entry stored with status in it, go to Hotel microservice, if it
fails then come back to Flight and cancel it and update DB status too.
- Microservice level duplicate how will you handle - Eg: If customer1 send request to buy fund,
- backend processing is done but response not recieved, meanwhile network issues shut down your
- machine (Maybe local issue or server crashes), customer1 comes back and place same request again.
- Now request is diff to app, how will you prevent such scenario ?
Ans: You definately will have DB entry with say fund name, amount and customer id. For every request,
check for combination of this 3 fields in DB in last say 1 hour. If you find entry having same amount, same
fund and custid, it can be this scenario. Return message saying "Duplicate request found, please try again."
He said: there is a term for handling this duplicate. what is that.
I: I dont know.
- What is saga** Round 2:**
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
//Input:
/**
Design your own logger api. Where you need to print the request Id based on Start time sorting order.
For example here request 2 start first but end at 4 but req1 starts late at 3 and end early at 3.30. Here output should be based on startTime sorting order and request those ended before other requst should be printed immediately. Here Request2 will be printed first at 4PM annd Request1 was in queue since it ended at 3.30 so should be printed immediatelt once request2 gets printed.
Id startTime endTime
req1 3 oclock 3.30 oclock
req2 2 oclock 4 pm
*/
Code:
public class Logger {
//O(log n)
public Map<String, String> tm = new TreeMap<>();
//approach 2:
public Stack<String> s= new Stack<>();
// req -
// public Map<Long, Object> tm1 = new TreeMap<>();
public void startEvent(String reqId, long sTime ){
tm.put(sTime, reqId); //o(log N);
/*List<Map.Entry<String, String>> list = new LinkedList<Map.Entry<String, String>>(tm.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, String>>() { // N log(n)
public int compare(Map.Entry<String, String> o1,
Map.Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});*/
}
public void endEvent(String reqId, long endTime){
//tm: req2, req1
Queue<String> q = new Queue();
for(int i: tm){
q.put(tm.get(i)+"-"+i);
//[q.get(0) == req2-2, q.get(1 == req1-3]
}
String value = tm.get(reqId);
value = value + "-" + endTime;
tm.put(reqId, value);
long now = LocalDateTime.now();
if(now == endTime) //get current time
if(q.peek().split("-").get(0) == reqId)
//if(tm.get(0).reqId == reqId)
System.out.print("Request:: "+reqId + " Start Time::"+tm.get(reqId)+ " End time:: "+endTime);
// iterator over queue
/*for(Map.Entry<String, String> a: tm.entrySet()) {
String[] s = a.getValue().split("-"); // s=[startTime EndTime]
if(s!= null) {
if((Long.parseLong(s[1])) < now) {
System.out.print("Request:: "+reqId + " Start Time::"+tm.get(reqId)+ " End time:: "+endTime);
}
}
}*/
}
}
public static void main(String[] args) {
// req Id - 123 start time - <> end time - <>
startEvent("Req1", 2);
endEvent("Req1", 4);
startEvent("Req2", 3);
endEvent("Req2", 3);
}
}Round 3:
Return subsequent subarray having great sum.
For example: arr = [3,-1,0,-4, 6, -1, 2]. Total sum = 5.
Output: [6,-1, 2]. sum = 7. // Here total sum is greater than sum of entire array
Solution: step1: Calculate sum of elements one by one.
// elem sum
//3 3
//-1 2
//0 2
//-4 -2
//6 4
//-1 3
//2 5
step 2: break the array of -negative sum is found and keep moving forward
Question 2:
Design a shopping cart and calculate total prize including discounts company has to offer.
For example: Have a look at below diagram in image which have multiple level of discounts. Initially i had suggested to different table like boys, girls etc. But can be solved with Category table only

category:
id . . category . . parent Id . . discount
1 . c1 . . 2 . . 10
2 . . c2 . . 3 . . 3
3 . . c3 . .
4 . . c4
5. c5 . . 1 . .
0+10+3 + 0 =13%