Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts

Saturday, March 21, 2020

Where and how to start for your system design

In this post, we are trying to cover where to start for your System Design even it is a very small system.

During initial few years of a developer,  they are involved in only coding of a small component or fixing a bug. Then all of a sudden you get assigned to design a complete system on your own.
Or you start looking out for a job and get stuck while attending your first few design interviews.

The mantra to overcome this challenge is very simple and you can follow below basic steps which you usually heard a lot of times from your seniors. Just try to remember Software Development Life Cycles...

Step 1: First step is to make the requirement clarifications. As a developer we get a bad habit of skipping the requirement which really blocks us sometime while coding. We need to go back to the PMs and leads to clarify different use cases for a same component. The reason of this is lacking the step for requirement clarification.

There are 2 types of requirement, one is functional clarification which tells you how to use your system? You can draw a flow chart of this functionalities.

The other requirement is to write the non functional requirement of the system. Few major things that drives the future of y0our project are:-

1. Availability
2. Number of user to be supported/Number of requests per second
3. Latency of each functionality
4. Sync vs Async
5. Storage/Compute/Network requirements
6. Redundancy/Failures to tolerate


Step 1.5: Yes usually we don't have a separate step for this but it is critical for the systems which is having a legacy system and you want to enhance some the features of it. In that case, it is very crucial for you to dive deep on the legacy system, understand how it works and then go for this implementation. If you avoid this step, then it may so happen that your system cause a regression and which impacts your overall project timeline.

Step 2: If all of the information required for your project is clear to you then you should move to this steps. Otherwise you should keep going to PMs, seniors to clarify on the requirement. You start with identifying the core components of your entire system. Don't focus on the integration at this point but make it as modular as you can. Try to use your flowchart come with the components.

One you are done with identifying all of your components, you try to use flow chart and start integrating with different components for each use cases.

While designing the components you can also keep in mind what minimal data are required to communicate with different components. Try to keep data locally with your component.

You should not think at all how different methods should be and how implementation can be done in this step. As a developer you try to think from that perspective and you start making mistakes there.

Step3: In this step you are allowed to think how each component can be designed. Since you have identified all your components and integration points you can use those integration points and how to implement those.

You need to keep in mind that, all parameters you are trying to  use, make it generic enough and so that it can extended anytime in future. it may look very ugly to keep one or two variables for a class but thats okay since it would enable to extend your component in future.







Monday, March 14, 2016

Why wait-notify should be called in a loop in a production environment?


From Java Documentation,

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     } 
 
Some more deatils on spurious wakeup ca be found here.
 

Sunday, February 7, 2016

Correct Singleton Class implemnetaion

This is a very basic design pattern question asked by the interviewer. There are multiple ways to implement Singleton class using Java.

Here is a correct way of implementation:

===
class Singleton {
 private volatile static Singleton _instance;//do not initialize here

private Singleton() { //mark it private to prevent Singleton object to be instantiated from outside

}

public static Singleton getInstance() {
 if (_instance == null) { 
  synchronized (Singleton.class) { 
   if (_instance == null) { 
    _instance = new Singleton();
    }
   }
  }
 return _instance;
}
===

Hibernate interview questions

1. Difference between load and get?
2. How load works? What is proxy?
3. What are first level and second level caches? Are these caches configurable? Specially first level cache.
4. QueryByExample?
5. What is better Criteria or HQL? Why?
Answer:
a. HQL is to perform both select and non-select queries  but Criteria is only for select query, we cannot perform non-select operations using criteria
b. HQL is suitable for executing Static Queries where as Criteria is suitable for executing Dynamic Queries(Dynamic SQL is simply a SQL statement that is composed on the fly before being executed)
c. HQL doesn’t support pagination concept, but we can achieve pagination with Criteria
d. With Criteria we are safe with SQL Injection because of its dynamic query generation but in HQL as your queries are either fixed or parametrized, there is no safe from SQL Injection.

Also criteria queries is Java developer friendly, so Criteria is better.

6. Lazy Load vs Eager Load?
7. Primary key generators.


Will update more questions.

Java interview questions


1. What is re-entrant lock? Difference between re-entrant lock and synchronized keyword?

2. Use of CountDownLatch, CyclicBarrier, Semaphore, Re-entrant Lock?

3. Difference between Iterator and for loop

Source: https://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

for-each loop really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, below example,
--------------------
void exampleMethod(Collection<String> c) {

for (Iterator<String> i = c.iterator();

i.hasNext();

if (i.next().length() == 40)

i.remove();

}
--------------------
The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

3. Order of execution of static class level blocks, class level block and constructor.
4. Which is better interface or 100% Abstract Class and why? 
5. Return statement in try, catch and finally. 
6. Abstract class constructor.
7. Cloneable Interface.
8. How to write immutable class? What about having data object? why the class is made final?
9. What constructs JVM?
10. What happens when Thread1 is running locking obj1 and Thread2 is wating for obj1. And Thread1 throws exception?
11. Serialization? How to de-serialize a singleton object? How to make subclass not serializable though its super is serializable?
12. What implements Decorator pattern? One example?
13. What changes between Java 1.5 vs Java 1.6?
14 What is Concurrent HashMap?  How it is improving performance than synchronizedMap?
What is the method to increment segment size?
15. What is the difference between wait and sleep method in Java?
Ans:
a. wait call should be made  from synchronized context but not mandatory for sleep.
b. If wait acquires any lock say obj1 then it will release the lock(monitor) temporarily so that notifying thread continue. It will acquire the lock again once the other threads notifies the thread(calling wait method). but sleep will hold the lock(until interrupted).



Will update more questions soon. Please feel free to make comments on the answer.

Want to crack interview, follow these books and links

Algorithms

 I think Algorithms and Mathematics are just like brothers. Each solution raises hundreds of problem. This subject can not be confined into one book. But the below mentioned books/links will help you to improve your problem solving skills.

 1.a. Go through "Introduction to Algorithms" book by Cormen. Focus on Time/Space Complexity, Stack Linked List, Tree, Graph(BDS & DFS only) first. For bigger companies look at Greedy/DP chapters too.
 
 1.b. DS and Algo improvement/practice links: You should try to solve the problem by yourself. Write the complete code on paper. Run the test cases on paper code. Just type the same code and see if it passes 100% TCs.
www.geeksforgeeks.org [Search by Company Name and Start Preparing One by One]

 1.c. Practice Coding on https://www.hackerrank.com/ OR https://leetcode.com/ [Don't Subscribe initially, first try with the easy ones]

If you are a beginner filter with easy coding for Arrays, Stack, LinkedList, Tree. Once all are solved try to solve medium level questions. Also as said above, try to come up with your solution first, then look for solutions that are better and optimized.

 1.d. Other Links
http://www.careercup.com/page?pid=amazon-interview-questions
www.doctorinterview.com
www.dsalgo.com

There are other links available also for these. Use any one of those. Please try to solve each problem by your own first. It might take 2-3 days to get a solution for a problem initially. After few days/weeks of practice it will be a matter of few minutes.

Java

To improve your java skills you need to practice a lot by writing codes, debugging each step sincerely. For understanding the basics you can follow this book.

2.a. Sun Certified Programmer for Java 6 Study Exam 310-065 Guide by Katherine Sierra, Bert Bates


System Design
I followed below Youtube Channel and It was pretty detailed.

- https://www.youtube.com/c/TechDummiesNarendraL/playlists
- Follow EducativeIO [First Go through the free ones to understand how to answer a System Design Question in a systematic manner]

Operating System

 The below mentioned books will help you to understand/clear up basic concepts of OS. Also after reading and watching these books/video tutorials you will come to know how things are implemented internally in an OS.

3.a. Operating System by Gary Nutt
3.b. The design of Unix operating system by Maurice j Bach
3.c. Link: http://pages.cs.wisc.edu/~remzi/OSTEP/
3.d. Operating system videos in youtube.com by Prof P K Biswas, IIT KGP prof: http://www.youtube.com/watch?v=MaA0vFKt-ew


Networking

If you understand the basics of TCP/IP and detailed functionality of each layer you are 50% done with networking. The book mentioned in the 4.a. (see below) is very helpful to grasp TCP/IP knowledge.

4.a Computer Networking: A Top-Down Approach by James F. Kurose, Keith W. Ross