Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cracking the Coding Interview

Cracking the Coding Interview

Presentation given at University of Waterloo to help students prepare, know what to expect and perform well on coding interviews at the top technology companies

Avatar for Marcelo Cortes

Marcelo Cortes

January 15, 2018

Other Decks in Programming

Transcript

  1. - Phone - Video conference - Onsite with remote interviewer

    - Onsite on the whiteboard The Environment
  2. - Phone - Video conference - Onsite with remote interviewer

    - Onsite on the whiteboard - Onsite pair programming The Environment
  3. - They are very busy but must stop “working” to

    interview you! The Interviewer POV
  4. - They are very busy but must stop “working” to

    interview you! - They are not there to fail you. They need and want more teammates. They want you to pass! The Interviewer POV
  5. - They are very busy but must stop “working” to

    interview you! - They are not there to fail you. They need and want more teammates. They want you to pass! - They want to enjoy the interview. Have good discussions, maybe learn something new, maybe teach you something. The Interviewer POV
  6. Most importantly! They must answer the question: Do I want

    this person on my team? The Interviewer POV
  7. - Can you communicate your ideas? - How well do

    you know the programming language? What are they looking for?
  8. - Can you communicate your ideas? - How well do

    you know the programming language? - Are you coachable? What are they looking for?
  9. - Can you communicate your ideas? - How well do

    you know the programming language? - Are you coachable? - How familiar and efficient are you with the tools? What are they looking for?
  10. - Can you communicate your ideas? - How well do

    you know the programming language? - Are you coachable? - How familiar and efficient are you with the tools? - Do you know algorithms? What are they looking for?
  11. - Can you communicate your ideas? - How well do

    you know the programming language? - Are you coachable? - How familiar and efficient are you with the tools? - Do you know algorithms? - Can you code? Recursion? Iteration? Data structures? What are they looking for?
  12. - Can you communicate your ideas? - How well do

    you know the programming language? - Are you coachable? - How familiar and efficient are you with the tools? - Do you know algorithms? - Can you code? Recursion? Iteration? Data structures? - Code style? Naming? Strategy? What are they looking for?
  13. 60 minutes 00..02: Quick intro and get setup 02..05: Describing

    the coding problem Coding Interview Structure
  14. 60 minutes 00..02: Quick intro and get setup 02..05: Describing

    the coding problem 05..15: Discussing algorithm and possible solutions Coding Interview Structure
  15. 60 minutes 00..02: Quick intro and get setup 02..05: Describing

    the coding problem 05..15: Discussing algorithm and possible solutions 15..55: Coding the solution Coding Interview Structure
  16. 60 minutes 00..02: Quick intro and get setup 02..05: Describing

    the coding problem 05..15: Discussing algorithm and possible solutions 15..55: Coding the solution 55..60: Candidate’s turn to ask questions Coding Interview Structure
  17. Given a list of words, find the maximum common prefix

    between any 2 words in the input list A Simple Problem
  18. - What is the alphabet? Lowercase a to z? ASCII?

    Unicode? - Do we expect the input to be very large? - Do we expect average string length to be large? - What if there is a tie? - Is null or empty input expected? Questions to Ask
  19. The Algorithm Is there an obvious brute force (inefficient) solution?

    YES! Mention it and quickly explain how the algorithm works.
  20. Is there an obvious brute force (inefficient) solution? YES! Mention

    it and quickly explain how the algorithm works. “We can compare each word to every other word and keep track of the largest common prefix found and its length” The Algorithm
  21. - Almost always the answer is YES! - Best way

    to get good at algorithms is practicing! - Always ask questions such as: - Sorting the input helps? - What data structures could help? - Set? Hashmap? Tree? - Can I collect data to help in a first pass? Can we do better?
  22. METHOD NAME: findMaxCommonPrefix INPUT: array of strings OUTPUT: string In

    Java: String findMaxPrefix(List<String> words){…} Define the Interface
  23. void validate(String[] words, String expected) { String prefix = findMaxPrefix(words);

    if (!expected.equals(prefix)) { throw new AssertionError( String.format("Wrong result for %s. “ + “Expected `%s` vs `%s` actual", Arrays.toString(words), expected, prefix)); } } String findMaxPrefix(String[] words) {...} Validation Framework
  24. Write Simple Test Cases public static void main(String[] args) {

    validate(new String[] {"electronics", "compact disc", "alphabet", "compass", "giraffe", "electricity"}, "electr"); validate(new String[] {"electronics"}, ""); validate(new String[] {"electronics", "electronicz"}, "electronic"); validate(new String[] {}, ""); System.out.println("All tests passed!"); }
  25. public static void main(String[] args) { validate(new String[] {"electronics", "compact

    disc", "alphabet", "compass", "giraffe", "electricity"}, "electr"); validate(new String[] {"electronics"}, ""); validate(new String[] {"electronics", "electronicz"}, "electronic"); validate(new String[] {}, ""); System.out.println("All tests passed!"); } All tests passed! Write Simple Test Cases
  26. public static String findMaxPrefixBruteForce(String[] words) { String maxPrefix = "";

    for (int i = 0; i < words.length; i++) { for (int j = 0; j < words.length; j++) { if (i == j) continue; String commonPrefix = maxCommonPrefix(words[i], words[j]); if (commonPrefix.length() > maxPrefix.length()) { maxPrefix = commonPrefix; } } } return maxPrefix; } private static String maxCommonPrefix(String left, String right) {..} Brute Force
  27. private static String maxCommonPrefix(String left, String right) { StringBuilder resultBuilder

    = new StringBuilder(); for (int i = 0; i < left.length() && i < right.length(); i++) { if (left.charAt(i) != right.charAt(i)) break; resultBuilder.append(left.charAt(i)); } return resultBuilder.toString(); } Brute Force
  28. public static String findMaxPrefixHashmap(String[] words) { String maxPrefix = "";

    Map<String, Integer> allPrefixes = new LinkedHashMap<>(); for (int i = 0; i < words.length; i++) { String word = words[i]; StringBuilder prefixBuilder = new StringBuilder(); for (int j = 0; j < word.length(); j++) { // Store the prefix and count. See next } } return maxPrefix; } Using Hashmap
  29. .. for (int j = 0; j < word.length(); j++)

    { prefixBuilder.append(word.charAt(j)); String prefix = prefixBuilder.toString(); Integer count = allPrefixes.get(prefix); if (count == null) { allPrefixes.put(prefix, 1); } else { count++; if (maxPrefix.length() < prefix.length()) { maxPrefix = prefix; } } } .. Using Hashmap
  30. public static String findMaxPrefixTree(String[] words) { String maxPrefix = "";

    TreeNode root = new TreeNode(); for (int i = 0; i < words.length; i++) { TreeNode current = root; boolean matching = true; String word = words[i]; StringBuilder prefixBuilder = new StringBuilder(); for (int j = 0; j < word.length(); j++) { // Add each word to the tree keeping track of max prefix. (See next) } } return maxPrefix; } Using Trie (prefix tree)
  31. .. for (int j = 0; j < word.length(); j++)

    { Character letter = word.charAt(j); TreeNode child = current.children.get(letter); if (child != null) { if (matching) { prefixBuilder.append(letter); if (prefixBuilder.length() > maxPrefix.length()) { maxPrefix = prefixBuilder.toString(); } } } else { matching = false; child = new TreeNode(); current.children.put(letter, child); } current = child; } .. Using Trie (prefix tree continuation...)
  32. Don’t ask: - How you did in the interview, your

    solution, other solutions - Better ways to solve the problem. - Salary, Bonus, Options, Etc. Do ask: - About the company, team, the product, the job - The tools, the work environment, the mission - How the team prioritize tasks, etc Your turn to ask questions
  33. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. Conclusion
  34. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. - Arrive early. Have your environment ready. Do not waste time on setup, that will take time away from your interview. Conclusion
  35. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. - Arrive early. Have your environment ready. Do not waste time on setup, that will take time away from your interview. - Communicate well and often. Ask questions. Keep talking. Conclusion
  36. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. - Arrive early. Have your environment ready. Do not waste time on setup, that will take time away from your interview. - Communicate well and often. Ask questions. Keep talking. - Have a strategy. Discuss options. If stuck, ask for advice. Conclusion
  37. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. - Arrive early. Have your environment ready. Do not waste time on setup, that will take time away from your interview. - Communicate well and often. Ask questions. Keep talking. - Have a strategy. Discuss options. If stuck, ask for advice. - Make sure you understand the problem. Write a simple test framework. Start with a test case. Conclusion
  38. - Practice a lot! Be prepared! That’s the best way

    to be calm and perform well. - Arrive early. Have your environment ready. Do not waste time on setup, that will take time away from your interview. - Communicate well and often. Ask questions. Keep talking. - Have a strategy. Discuss options. If stuck, ask for advice. - Make sure you understand the problem. Write a simple test framework. Start with a test case. HAVE FUN! Conclusion