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

SE103 - Week 10, Session 1

Caren
August 08, 2020

SE103 - Week 10, Session 1

Caren

August 08, 2020
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. Week 10:
    Backtracking

    View Slide

  2. How was the HackerRank?

    View Slide

  3. What is Backtracking?

    View Slide

  4. What is Backtracking?
    Enter
    Exit

    View Slide

  5. What is Backtracking?
    Enter
    Exit
    What are we trying to solve?

    -> Ways to exit maze

    View Slide

  6. What is Backtracking?
    Enter
    Exit
    What are we trying to solve?

    -> Ways to exit maze
    1. Have we reached the end?

    -> If yes, we found a route!

    -> If no, try more paths (2)

    View Slide

  7. What is Backtracking?
    Enter
    Exit
    What are we trying to solve?

    -> Ways to exit maze
    1. Have we reached the end?

    -> If yes, we found a route!

    -> If no, try more paths (2)
    2. Go to next intersection / try
    next available routes

    View Slide

  8. What is Backtracking?
    Enter
    Exit
    What are we trying to solve?

    -> Ways to exit maze
    1. Have we reached the end?

    -> If yes, we found a route!

    -> If no, try more paths (2)
    2. Go to next intersection / try
    next available routes
    3. For each unvisited
    direction, try to solve again

    View Slide

  9. What is Backtracking?
    Enter
    Exit
    What are we trying to solve?

    -> Ways to exit maze
    1. Have we reached the end?

    -> If yes, we found a route!

    -> If no, try more paths (2)
    2. Go to next intersection / try
    next available routes
    3. For each unvisited
    direction, try to solve again
    4. If dead end, go back to last intersection

    View Slide

  10. - Backtracing: Uses constraints to make search
    more efficient. Usually looks for all answers that
    satisfy constraint.
    - Dynamic Programming: Looking for the most
    optimal solution
    Backtracking vs DP

    View Slide

  11. 1. Search - what are we searching / solving for?
    2. GetCandidates - get all the possible potential
    solutions (local solutions)
    3. IsSolution - is this candidate a solution?
    4. Backtrack - Undo any state that was caused with
    local guesses
    Common Backtracking Algorithm

    View Slide

  12. Given a 2D board and a word, find if the word exists in the grid. 


    The word can be constructed from letters of sequentially adjacent cell,
    where "adjacent" cells are those horizontally or vertically neighboring. The
    same letter cell may not be used more than once.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    “see” -> true
    “xyz” -> false
    Word Search

    View Slide

  13. Given a 2D board and a word, find if the word exists in the grid. 


    The word can be constructed from letters of sequentially adjacent cell,
    where "adjacent" cells are those horizontally or vertically neighboring. The
    same letter cell may not be used more than once.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    “see” -> true
    “xyz” -> false
    Word Search
    What are our constraints?

    View Slide

  14. Given a 2D board and a word, find if the word exists in the grid. 


    The word can be constructed from letters of sequentially adjacent cell,
    where "adjacent" cells are those horizontally or vertically neighboring.
    The same letter cell may not be used more than once.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    “see” -> true
    “xyz” -> false
    Word Search
    constraints

    View Slide

  15. Given a 2D board and a word, find if the word exists in the grid.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    Word Search
    isSolution(prefix, word)
    // prefix is our built up solution
    // return prefix == word
    getCandidates(position, visited, board, letter)

    // get unvisited adjacent positions that match
    letter

    backup(prefix, position, visited)

    // remove last letter from previous

    // remove position from visited

    search(board, word, prefix, position, visited)

    // return true if isSolution(prefix, word)

    // for each getCandidates

    // add letter(position) to prefix

    // add position to visited

    // search from next position

    // if not found backup

    View Slide

  16. Given a 2D board and a word, find if the word exists in the grid.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    Word Search
    isSolution(prefix, word)

    // prefix is our built up solution

    // return prefix == word

    getCandidates(position, visited, board, letter)
    // get unvisited adjacent positions that match
    letter
    backup(prefix, position, visited)

    // remove last letter from previous

    // remove position from visited

    search(board, word, prefix, position, visited)

    // return true if isSolution(prefix, word)

    // for each getCandidates

    // add letter(position) to prefix

    // add position to visited

    // search from next position

    // if not found backup

    View Slide

  17. Given a 2D board and a word, find if the word exists in the grid.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    Word Search
    isSolution(prefix, word)

    // prefix is our built up solution

    // return prefix == word

    getCandidates(position, visited, board, letter)

    // get unvisited adjacent positions that match
    letter

    backup(prefix, position, visited)
    // remove last letter from previous
    // remove position from visited
    search(board, word, prefix, position, visited)

    // return true if isSolution(prefix, word)

    // for each getCandidates

    // add letter(position) to prefix

    // add position to visited

    // search from next position

    // if not found backup

    View Slide

  18. Given a 2D board and a word, find if the word exists in the grid.
    board = [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]
    Word Search
    isSolution(prefix, word)

    // prefix is our built up solution

    // return prefix == word

    getCandidates(position, visited, board, letter)

    // get unvisited adjacent positions that match
    letter

    backup(prefix, position, visited)

    // remove last letter from previous

    // remove position from visited

    search(board, word, prefix, position, visited)
    // return true if isSolution(prefix, word)
    // for each getCandidates
    // add letter(position) to prefix
    // add position to visited
    // search from next position
    // if not found backup

    View Slide

  19. Debug Session
    Combination Sum

    View Slide

  20. Next Session
    - DP meets Backtracking!

    View Slide

  21. Breakout Rooms
    - Half of group does one problem, other half does the
    other problem

    - 30 minutes, everybody tries to do their own problem

    - 10 minutes - group 1 explains their problem

    - 10 minutes - group 2 explains their problem

    - Last 10 minutes - try implementing the problem we
    just debugged together!

    View Slide

  22. Hiring Behind the Scenes
    What happens after you finish that onsite interview??

    View Slide

  23. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    View Slide

  24. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    View Slide

  25. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    - Was the person’s coding skills up to par? (did they understand
    time / space complexity, do they know when to use different data
    structures, etc)


    View Slide

  26. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    - Was the person’s coding skills up to par? (did they understand
    time / space complexity, do they know when to use different data
    structures, etc)

    - Was the person able to communicate well?


    View Slide

  27. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    - Was the person’s coding skills up to par? (did they understand
    time / space complexity, do they know when to use different data
    structures, etc)

    - Was the person able to communicate well?

    - Was the person respectful of everybody?

    View Slide

  28. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    - Was the person’s coding skills up to par? (did they understand
    time / space complexity, do they know when to use different data
    structures, etc)

    - Was the person able to communicate well?

    - Was the person respectful of everybody?

    - Is this someone we would enjoy working with?


    View Slide

  29. Hiring Behind the Scenes
    Startup World:

    - Everybody individually enters their feedback right after they talk to
    you (hire / no hire / not sure)

    - Everybody gets together to talk as a group

    - Was the person’s coding skills up to par? (did they understand
    time / space complexity, do they know when to use different data
    structures, etc)

    - Was the person able to communicate well?

    - Was the person respectful of everybody?

    - Is this someone we would enjoy working with?

    - Does the person’s background match what we need in the
    team?

    View Slide

  30. Hiring Behind the Scenes
    Big Company World

    View Slide

  31. Hiring Behind the Scenes
    Big Company World

    - Interviewer enters their feedback immediately after


    View Slide

  32. Hiring Behind the Scenes
    Big Company World

    - Interviewer enters their feedback immediately after

    1. Coding

    2. Design

    3. Communication

    4. Speed

    View Slide

  33. Hiring Behind the Scenes
    Big Company World


    - Coding skills

    - is your code easy to understand?

    - did you handle edge cases?

    - do you know when to use data structures and built in methods?

    - were you able to solve your own bugs?

    - Design

    - Communication

    - Speed


    View Slide

  34. Hiring Behind the Scenes
    Big Company World

    - Coding

    - Design

    - were you able to gather enough requirements to solve the problem?

    - were you able to discuss trade-offs with different approaches?

    - were you able to verbally explain a solution before jumping into
    implementation?

    - did you use the appropriate data structures / algorithms?

    - Communication

    - Speed


    View Slide

  35. Hiring Behind the Scenes
    Big Company World


    - Coding

    - Design

    - Communication

    - were you able to understand the problem and state possible
    confusions?

    - did what you say match what you coded?

    - Speed


    View Slide

  36. Hiring Behind the Scenes
    Big Company World


    - Coding

    - Design

    - Communication

    - Speed

    - were you able to finish the question within the expected time?
    (often times a question has followups)

    - did you need hints to guide you to the solution?

    View Slide