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

Preparing the tech interview

Preparing the tech interview

In this talk we'll start with a basic introduction to algorithm complexity analysis and then we'll see a couple of examples of interview questions.

Andrea Iacono

February 15, 2017
Tweet

More Decks by Andrea Iacono

Other Decks in Programming

Transcript

  1. Preparing the Tech Interview: intro Big tech companies: - Google,

    Amazon, Facebook, Apple Online giants: - Booking, TripAdvisor, LinkedIn, PayPal, eBay, Dropbox, … Tech companies: - Red Hat, Oracle, Microsoft, LightBend, Cloudera, Elastic, ... We’ll focus on algorithms and data structures
  2. Preparing the Tech Interview: complexity analysis Big O Notation a

    symbolism to describe the asymptotic behaviour of functions Examples: O(n·log n), O(2n) - it’s relative to the input: on x axis the input size, on y axis the time - it’s an upper bound - we’re interested only in the highest exponential: O(n2 + n2/2 + 100n) = O(n2)
  3. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
  4. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? Runtime complexity is O(n) int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
  5. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? Runtime complexity is O(n) And which is the space complexity? int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
  6. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? Runtime complexity is O(n) And which is the space complexity? Space complexity is O(1) int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
  7. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? int sum(int n) { int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; }
  8. Preparing the Tech Interview: complexity examples int sum(int n) {

    int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? Runtime complexity is O(n)
  9. Preparing the Tech Interview: complexity examples int sum(int n) {

    int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? And which is the space complexity? Runtime complexity is O(n)
  10. Preparing the Tech Interview: complexity examples int sum(int n) {

    int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? And which is the space complexity? Space complexity is O(n) Runtime complexity is O(n)
  11. Preparing the Tech Interview: complexity examples int sum(int n) {

    int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; } Which is the runtime complexity of this method?
  12. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? Runtime complexity is O(n) int sum(int n) { int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; }
  13. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? And which is the space complexity? Runtime complexity is O(n) Runtime complexity is O(n) int sum(int n) { int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; }
  14. Preparing the Tech Interview: complexity examples Which is the runtime

    complexity of this method? And which is the space complexity? Space complexity is O(1) Runtime complexity is O(n) Runtime complexity is O(n) int sum(int n) { int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; }
  15. Preparing the Tech Interview: complexity examples int sum(int n) {

    return n * (n+1) / 2; } Which is the runtime complexity of this method?
  16. Preparing the Tech Interview: complexity examples int sum(int n) {

    return n * (n+1) / 2; } Which is the runtime complexity of this method? Runtime complexity is O(1)
  17. Preparing the Tech Interview: complexity examples int sum(int n) {

    return n * (n+1) / 2; } Which is the runtime complexity of this method? Runtime complexity is O(1) And which is the space complexity?
  18. Preparing the Tech Interview: complexity examples int sum(int n) {

    return n * (n+1) / 2; } Which is the runtime complexity of this method? Runtime complexity is O(1) And which is the space complexity? Space complexity is O(1)
  19. Preparing the Tech Interview: complexity examples void bubbleSort(int[] array) {

    int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method?
  20. Preparing the Tech Interview: complexity examples void bubbleSort(int[] array) {

    int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2)
  21. Preparing the Tech Interview: complexity examples void bubbleSort(int[] array) {

    int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2) And which is the space complexity?
  22. Preparing the Tech Interview: complexity examples void bubbleSort(int[] array) {

    int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2) And which is the space complexity? Space complexity is O(1)
  23. Preparing the Tech Interview: complexity examples int fibonacci(int n) {

    if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method?
  24. Preparing the Tech Interview: complexity examples int fibonacci(int n) {

    if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method? Runtime complexity is O(2n)
  25. Preparing the Tech Interview: complexity examples int fibonacci(int n) {

    if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method? Runtime complexity is O(2n) And which is the space complexity?
  26. Preparing the Tech Interview: complexity examples int fibonacci(int n) {

    if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method? Runtime complexity is O(2n) And which is the space complexity? Space complexity is O(1)
  27. Preparing the Tech Interview: what to study General Knowledge Data

    Structures: - Arrays - Trees, BST, Auto-Balancing Trees - Linked Lists - Maps - Stacks / Queues - Priority Queues / Heaps - Graphs
  28. Preparing the Tech Interview: what to study General Knowledge Algorithms

    / Approaches: - Sorting (Quick Sort, Merge Sort, non-comparison Sort) - Trees (Pre-Order, In-Order, Post-Order) - Binary Search - Graph (DFS, BFS, Dijkstra) - Backtracking - Dynamic Programming - Computational Geometry
  29. Preparing the Tech Interview: brain teaser? How many golf balls

    would fit in this room? - Golf ball radius is about 2.5cm - Sphere volume is: 4/3 · π · r3 - Golf ball volume is about 4/3 · 3.14 · 15 ~= 60 cm3
  30. Preparing the Tech Interview: brain teaser? How many golf balls

    would fit in this room? - Golf ball radius is about 2.5cm - Sphere volume is: 4/3 · π · r3 - Golf ball volume is about 4/3 · 3.14 · 15 ~= 60 cm3 - This room is about 10m · 20m · 3m - Room volume is about 600 m3
  31. Preparing the Tech Interview: brain teaser? How many golf balls

    would fit in this room? - Golf ball radius is about 2.5cm - Sphere volume is: 4/3 · π · r3 - Golf ball volume is about 4/3 · 3.14 · 15 ~= 60 cm3 - This room is about 10m · 20m · 3m - Room volume is about 600 m3 600 m3 / 60cm3 = 600 · 106 cm3 / 60cm3 ≈ 107 golf balls
  32. Preparing the Tech Interview: brain teaser? There are three boxes,

    one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly?
  33. Preparing the Tech Interview: brain teaser? There are three boxes,

    one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O A O A+O A A+O O O A+O A O A A+O A+O A O A+O O A
  34. Preparing the Tech Interview: brain teaser? There are three boxes,

    one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O A O A+O A A+O O O A+O A O A A+O A+O A O A+O O A
  35. Preparing the Tech Interview: brain teaser? There are three boxes,

    one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O O A+O A A+O A O
  36. Preparing the Tech Interview: brain teaser? There are three boxes,

    one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O O A+O A A+O A O Just pick from the box labeled A+O and you’re sure about all the labels.
  37. Preparing the Tech Interview: references Online references • www.topcoder.com -

    for gaining speed (and some useful tutorials) • geeksforgeeks.org / leetcode.com - for detailed explanations of solutions • www.codingame.com - for problems in gaming environment • www.interviewbit.com - for following a gamificated program of practice • pramp.com – for practicing real interviews with peers