- http://leetcode.com/2011/09/regular-expression-matching.html
- How would you design a request dispatcher for load balancing ?
- http://www.javaworld.com/article/2077921/architecture-scalability/server-load-balancing-architectures--part-1--transport-level-load-balancing.html
- http://www.javaworld.com/article/2077922/architecture-scalability/server-load-balancing-architectures-part-2-application-level-load-balanci.html
- Detect spam reviews on yelp ?
- Anagram grouping
- Write code to generate all possible case combinations of a given lower-cased string. (e.g. "0ab" -> ["0ab", "0aB", "0Ab", "0AB"])
- http://algorithmsforinterview.blogspot.com/2012/08/print-given-string-in-all-combinations_14.html
- http://coding-interviewq.blogspot.com/2012/04/generate-all-substrings-of-string.html
- http://coding-interviewq.blogspot.com/2012/04/generate-all-permutations-of-string-in.html
- given an array of intervals, return max number of non-overlapping intervals
- Longest palindrome substring
- http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html
- http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
- Given a lower case string ab generate all lower case and upper case combinations
- Given a list of strings, write a function to generate longest common prefix of those strings.
- Explain SSL
- Explain DNS
- Given a list of urls, find out the top 5 urls
- Regex http://leetcode.com/2011/09/regular-expression-matching.html
Apart from coding and design interview questions, this page contains updates on my learnings with Java. It helps me organize my learning. Read about my future self here : https://siliconvalleystories.blogspot.com/
Tuesday 23 December 2014
Yelp Interview Questions
Friday 19 December 2014
Parsing XML files in JAVA
- Use the SAX parser for large files http://stackoverflow.com/questions/15132390/parsing-large-xml-documents-in-java
- http://elegantcode.com/2010/08/07/dont-parse-that-xml/
- The DOM Parser loads the complete XML content into a Tree structure. And we iterate through the Node and NodeList to get the content of the XML
- SAX Parser is different from the DOM Parser where SAX parser doesn’t load the complete XML into the memory, instead it parses the XML line by line triggering different events as and when it encounters different elements like: opening tag, closing tag, character data, comments and so on. This is the reason why SAX Parser is called an event based parser.
- StAX stands for Streaming API for XML and StAX Parser is different from DOM in the same way SAX Parser is. StAX parser is also in a subtle way different from SAX parser.The SAX Parser pushes the data but StAX parser pulls the required data from the XML.The StAX parser maintains a cursor at the current position in the document allows to extract the content available at the cursor whereas SAX parser issues events as and when certain data is encountered.
XMLInputFactory and XMLStreamReader are the two class which can be used to load an XML file. And as we read through the XML file using XMLStreamReader, events are generated in the form of integer values and these are then compared with the constants inXMLStreamConstants.
Garbage Collection in Java
References :
- http://mechanical-sympathy.blogspot.com/2013/07/java-garbage-collection-distilled.html
- http://www.infoq.com/articles/low-latency-vp
- http://stackoverflow.com/questions/2574579/what-is-your-development-checklist-for-java-low-latency-application
- http://programmers.stackexchange.com/questions/149563/should-we-avoid-object-creation-in-java
- http://mentablog.soliveirajr.com/2012/11/real-time-java-programming-without-gc/
- http://architects.dzone.com/articles/5-tips-proper-java-heap-size
Thursday 18 December 2014
Design Pattern Books
- Head First design patterns
- http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420/ref=sr_1_1?ie=UTF8&qid=1418969510&sr=8-1&keywords=enterprise+design+patterns
- http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=pd_sim_b_5?ie=UTF8&refRID=1PA3FYGRC0XE9XNGBFC1
- http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672/ref=pd_sim_b_4?ie=UTF8&refRID=1PA3FYGRC0XE9XNGBFC1
- http://www.amazon.com/The-Pragmatic-Programmer-Journeyman-Master/dp/020161622X/ref=pd_sim_b_7?ie=UTF8&refRID=0MSR01BNCHXFFFXPB6FQ
Design Pattern CheatSheet
Observer Pattern : The observer pattern defines a one to many relationship between objects so that when one object changes state all the other dependent objects are notified.
Factory Pattern : encapsulates object creatio. Defines an interface for creating an object, but lets subclass decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
Adapter Pattern : Converts interfaces of a class to another interface that clients expect. Lets classes work together that wouldnt otherwise because of incompatible interfaces.
Singleton - Use double check locking because the synchronization is only needed for the object instantiation part. If you already have an instance of that object, you dont need to do the synchronization. Declare the instance as static volatile, the constructor as private and a getInstance function as public static.
Command Pattern : encapsulates a request as an object , thereby letting you parameterize other objects with different requests, queue or log requests and support undoable operations. Light will have OnCommand and OffCommand and it will be there in the remote control. You can queue such incoming commands.Command pattern gives you a way to package a piece of computation or actions and the receiver and store it as a first class object.
Decorator Pattern : Doesnt alter the interface, but add responsibility to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. In the decorator design pattern the class or abstract class that is being extended is also present as a property in the class extending it.
Facade : Make interface simpler. The facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.
Strategy Pattern : Encapsulate interchangeable behavior and use delegation to decide which behavior to use.
Template Method : Defines the skeleton of an algorithm in a method in an abstract class, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
A hook is a method that is defined in an abstract class, but only given an empty or default implementation. This gives the subclass the ability to hook into the algorithm at various points, if they wish, a subclass is also free to ignore the hook.
Iterator Pattern : The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation
Observer Pattern - The observer interface is implemented by all observers. The update method has paramters which are received by all observers when the state of the subject changes.
WeatherData implements the Subject interface which registersObservers, UpdatesObservers, RemovesObservers. It also holds a list of observers.
The ConcreteObserver stores a reference to the subject where it registers. This is not necessarily needed. This functionality is given so that the observer can unregister from the subject if it feels like.
We say that a module has high cohesion when it is designed around a set of related functions and say it has low cohesion when it is designed around a set of unrelated functions. Classes that adhere to a single responsibility principle have high cohesion. Every responsibility of a class is an area of change. More than one responsibility means more than one area of change.
OO Principles :
Factory Pattern : encapsulates object creatio. Defines an interface for creating an object, but lets subclass decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
Adapter Pattern : Converts interfaces of a class to another interface that clients expect. Lets classes work together that wouldnt otherwise because of incompatible interfaces.
Singleton - Use double check locking because the synchronization is only needed for the object instantiation part. If you already have an instance of that object, you dont need to do the synchronization. Declare the instance as static volatile, the constructor as private and a getInstance function as public static.
Command Pattern : encapsulates a request as an object , thereby letting you parameterize other objects with different requests, queue or log requests and support undoable operations. Light will have OnCommand and OffCommand and it will be there in the remote control. You can queue such incoming commands.Command pattern gives you a way to package a piece of computation or actions and the receiver and store it as a first class object.
Decorator Pattern : Doesnt alter the interface, but add responsibility to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. In the decorator design pattern the class or abstract class that is being extended is also present as a property in the class extending it.
Facade : Make interface simpler. The facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.
Strategy Pattern : Encapsulate interchangeable behavior and use delegation to decide which behavior to use.
Template Method : Defines the skeleton of an algorithm in a method in an abstract class, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
A hook is a method that is defined in an abstract class, but only given an empty or default implementation. This gives the subclass the ability to hook into the algorithm at various points, if they wish, a subclass is also free to ignore the hook.
Iterator Pattern : The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation
Observer Pattern - The observer interface is implemented by all observers. The update method has paramters which are received by all observers when the state of the subject changes.
WeatherData implements the Subject interface which registersObservers, UpdatesObservers, RemovesObservers. It also holds a list of observers.
The ConcreteObserver stores a reference to the subject where it registers. This is not necessarily needed. This functionality is given so that the observer can unregister from the subject if it feels like.
We say that a module has high cohesion when it is designed around a set of related functions and say it has low cohesion when it is designed around a set of unrelated functions. Classes that adhere to a single responsibility principle have high cohesion. Every responsibility of a class is an area of change. More than one responsibility means more than one area of change.
OO Principles :
- Encapsulate what varies (encapsulating object creation is factory, encapsulating method invocation is , encapsulating algorithms is template pattern, encapsulate iteration over different data types is iteration, )
- Favor composition over inheritance
- Program to interfaces not implementations
- Strive for loosely coupled designed between objects that interact
- Classes should be open for extension but closed for modification
- Depend on abstractions and do not depend on concretions
- Only talk to friends (Facade pattern and principle of least knowledge)
- Dont call us, we will call you (Hollywood principle, Template)
- Dependency Inversion : Avoid the use of concrete classes and instead work with as much abstractions as possible.
- Principle of least knowledge : Objects should talk to only few friends. The principle prevents our system such that we have large number of classes coupled together so that changes to one part of the system cascade to other parts. When you build a lot of dependencies between many classes you build a fragile system that will be costly to maintain and difficult for others to understand.
- Hollywood Principle : Dependency rots happen when you have high level components depending on low level components depending on high level components depending on sideways components depending on low level components and so on. We allow low level components to hook themselves into a system, but the high level components determine when they are needed and how. In other words, the high level components give the low level components a "Dont call us, we will call you.".
- Open Principle : Classes should be open for extension but closed for modification - decorator pattern
Wednesday 17 December 2014
Convert JSON to Java Objects and vice versa
This post contains performance comparison of relevant libraries.
Add the json here and generate all the classes needed to parse the java objects : http://www.jsonschema2pojo.org/
Parse json array to arraylist of objects : http://www.leveluplunch.com/java/examples/convert-json-array-to-arraylist-of-objects-jackson/
http://www.developer.com/lang/jscript/top-7-open-source-json-binding-providers-available-today.html
Add the json here and generate all the classes needed to parse the java objects : http://www.jsonschema2pojo.org/
Parse json array to arraylist of objects : http://www.leveluplunch.com/java/examples/convert-json-array-to-arraylist-of-objects-jackson/
http://www.developer.com/lang/jscript/top-7-open-source-json-binding-providers-available-today.html
Tuesday 16 December 2014
Leetcode : Candy Java
Scan the rating array from left to right and then from right to left. In every scan just consider the rising order (l->r: r[i]>r[i-1] or r->l: r[i]>r[i+1]), assign +1 candies to the rising position.
The final candy array is the maximum (max(right[i],left[i])) in each position.
The total candies is the sum of the final candy array.
Monday 15 December 2014
Leetcode : Maximum Product Subarray
public class Solution {
public int maxProduct(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int max = A[0], min = A[0], result = A[0];
for (int i = 1; i < A.length; i++) {
int temp = max;
max = Math.max(Math.max(max * A[i], min * A[i]), A[i]);
min = Math.min(Math.min(temp * A[i], min * A[i]), A[i]);
if (max > result) {
result = max;
}
}
return result;
}
}
Saturday 13 December 2014
Amazon Interview Set 159
- How to implement sets ?
- http://web.cs.wpi.edu/~cs2102/b12/Lectures/bsts-and-avls.html
- http://stackoverflow.com/questions/2630738/c-how-to-implement-set-data-structure
- Quick Sort in Java
- Rotate a 2D array
- Buy and Sell stock
- Kth largest elements in an array
- http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html
- http://www.geeksforgeeks.org/given-sorted-dictionary-find-precedence-characters/
- Generics and Collections in Java
- Head First design patterns
- Effective Java
Subscribe to:
Posts (Atom)