LightRider
BAN USERAvoid jumping right into class diagram and code level implementation in the beginning. The best way to approach such open-ended questions during Interview is to divide the entire designing process into two parts:
Note that High-Level Design techniques have a pretty straight forward. Having a hold on these and starting with these give you an upper hand.
a) High-Level Design
- Start building bottoms up.
- Discuss whether your Application is read-heavy or write-heavy. {Extract out the various operations}
- Try to understand how your system will communicate with different components.
- Discuss what all databases you will use, give reasons for them. {SQL/NOSQL}
- Discuss which two things out of three in CAP theorem interviewer wants you to focus on.
- Present ways how you can scale the system in case of heavy traffic.
# Caching approach
# Sharding database
# Adding Read replicas
# Discuss if you can use CDN for lesser network hops
b) Low-Level Design
- Select a component for which you want to discuss a LLD.
- Start with Class diagrams.
- Discuss the various use cases. If possible present a use case diagram.
- List down a few APIs relevant to this service.
- Now start introducing Design Patterns. {Factory, Observer, Chain of responsibility are few commonly applicable in most of the systems}
- If possible discuss on the SOLID design principles.
I don't know how all of you missed this point since the new products are added, means the cache is not warm and all the queries pertaining to the new product are hitting the database now. This will result in increased load on DB operations.
Hence more DB queries, slower data replication, and hence slower system.
This problem could be solved with the help of HashMap<Integer, Integer>.
- LightRider April 26, 2017Now suppose we have data from N users given to us. Our aim is to find List of hotels common to all the users.
For the 1st user: {2,3,1}
* Add the hotel-Id directly as key and instance-count as value. At the first iteration the map will look something like this:
2-->1
3-->1
1-->1
For the 2nd user: {2,5,3}
* Now check if the hotel-Id present in the hashMap, if not then ignore the element given, otherwise extract the data from the map and add (user-Id*hotel-Id) to the previous value. After the second iteration the map looks like this:
2-->3 {1 + (2*1)}
3-->3 {1 + (2*1)}
1-->1 {nothing happens to this, since this was absent in the second user list}
For the 3rd user: {7,3,1}
* Similar to the 2nd user we update the values for the common elements between the map keys and given hotel-Id array. After the third iteration the map looks like this:
2-->3 {nothing happens to this since this was absent in the second user list}
3-->6 {3 + (3*1)}
1-->1 {nothing happens to this since this was absent in the second user list}
similarly, iterate till the end of all the user arrays given.
Now at the end, we just need to figure out those keys which have value {n*(n+1)/2}. So for the above case, the result is {3}
RUN TIME: O(N) {where N is the sum of length of all the hotel-Ids}
SPACE: O(length of elements in first hotel-Id array)
Sorry for the lengthy explanation, but I feel I have made the logic clear. Feel free to drop comments in case of doubts.
Thanks