Designing Distributed Systems

Java Interview - Code Snippets to remember

Before going to a java interview it's best to remember a few coding snippets that would greatly help you to compose your coding interview answers.  Here are some coding snippets I have reached to, I encourage you to add in comments other snippets you think are important.


  1. String
    1. char c = s.charAt(2); // get char at location 2.
    2. “hamburger”.substring(4,8) // startindex0 so from idx 4 to idx 8
  2. Use StringBuilder for new string building setCharAt .append
  3. Map
    1. map.containsKey(k) // check if k is in map
    2. 537 % 10 # => 7 extract first digit from number
    3. 537 / 10 # => 53 remove first digit from number
  4. List
    1. add(index, element)
    2. set(index, element)
    3. remove(index)
  5. Random
  6. new Random.nextInt(10) // 0 - 9 (exclusive)
  7. Queue,Stack - start using those data structures!!
  8. Arrays:
    1. Copy
      1. System.arraycopy(source, startindex, dest, startindex, nToCopy)
    2. Print
      1. Arrays.toString(d.getArr())

BOOK: "Elements of Programming Interviews in Java: The Insiders' Guide" This is the best book I have found on java interview tips, if you are looking to concentrate on java this is by far the best resource on the subject.



Comments