Pages

Thursday, 8 January 2015

Generate all permutations of a string in Java

How would you solve the problem recursively ? 
 
Suppose you have a permutation(n-1) which returns all permutations of size n-1. You will 
pass  
for (int i = 0; i < n; i++)
           permutation(str.substring(0, i) + str.substring(i+1, n)); 
 
Then to every returned string you will add str.charAt(i).
 
So you have to deal with an array of strings in return type.
Instead you pass the str.charAt(i) as a part of the input itself and you dont 
need to worry about concatenating it at the end.
 
Instead when the length of the second parameter in the permutation becomes 0 
you just print the first paramter.
       
public  static void permutation(String str) { 
    permutation("", str); 
 }

 private static void permutation(String prefix, String str) {
    int n = str.length();
    if (n == 0) System.out.println(prefix);
    else {
        for (int i = 0; i < n; i++)
           permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
    }
}
 
 
Now you have done it with strings and you are printing it. Suppose you have to return a 
List<List<Integer>> then how would you do it. Practise this as it will improve your collections.


public class Solution {
    List<List<Integer>> ret;
    public List<List<Integer>> permute(int[] num) {
        ret = new LinkedList<>();
        LinkedList<Integer> numbers = new LinkedList<>();
        for (int i = 0; i < num.length; i++)
            numbers.add(num[i]);
        LinkedList<Integer> list = new LinkedList<>();
        permute(numbers, list);
        return ret;
    }
    
    private void permute(List<Integer> numbers, List<Integer> list) {
        if (numbers.size() == 0) {
            LinkedList<Integer> newList = new LinkedList<>();
            newList.addAll(list);
            ret.add(newList);
            return;
        }
        for (int i = 0; i < numbers.size(); i++) {
            int candidate = numbers.get(i);
            numbers.remove(i);
            list.add(candidate);
            permute(numbers, list);
            list.remove(list.size() - 1);
            numbers.add(i, candidate);
        }
    }

Backtracking

Tuesday, 23 December 2014

Yelp Interview Questions

  1. http://leetcode.com/2011/09/regular-expression-matching.html
  2. How would you design a request dispatcher for load balancing ?
    1. http://www.javaworld.com/article/2077921/architecture-scalability/server-load-balancing-architectures--part-1--transport-level-load-balancing.html 
    2. http://www.javaworld.com/article/2077922/architecture-scalability/server-load-balancing-architectures-part-2-application-level-load-balanci.html 
    3.  
  3. Detect spam reviews on yelp ?
  4. Anagram grouping
  5. Write code to generate all possible case combinations of a given lower-cased string. (e.g. "0ab" -> ["0ab", "0aB", "0Ab", "0AB"])  
    1. http://algorithmsforinterview.blogspot.com/2012/08/print-given-string-in-all-combinations_14.html 
    2. http://coding-interviewq.blogspot.com/2012/04/generate-all-substrings-of-string.html 
    3. http://coding-interviewq.blogspot.com/2012/04/generate-all-permutations-of-string-in.html 
  6. given an array of intervals, return max number of non-overlapping intervals
  7. Longest palindrome substring
    1. http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 
    2. http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 
  8. Given a lower case string ab generate all lower case and upper case combinations
  9. Given a list of strings, write a function to generate longest common prefix of those strings.
  10. Explain SSL
  11. Explain DNS
  12. Given a list of urls, find out the top 5 urls
  13. Regex http://leetcode.com/2011/09/regular-expression-matching.html
  14.  

Friday, 19 December 2014

Parsing XML files in JAVA

  1. Use the SAX parser for large files http://stackoverflow.com/questions/15132390/parsing-large-xml-documents-in-java
  2. http://elegantcode.com/2010/08/07/dont-parse-that-xml/
  3. 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
  4. 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.
  5. 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.
  1.  

Garbage Collection in Java