Cracking the Coding Interview

Technical Interviews
Shane Carr
CSE 232, September 11, 2015
Letter Counter:
Language Breakdown
Rust
7%
Scala
7%
Java
46%
Python
40%
Letter Counter:
Approach to Sorting
Stable
40%
Combined
60%
Schedule for the next few weeks
In Lecture
 Today
 Interview Techniques
 Time Complexity
 Preview of Problem Analysis
 Next Week
 Problem Analysis
 Data Structures
 Next-Next Week
 Start on algorithms
Programming Contests
 This Weekend: HackerRank
CodeSprint!
 https://www.hackerrank.c
om/worldcup/
 Sign up by next Wednesday
at midnight: ICPC Qualifiers!
 https://docs.google.com/
forms/d/1J006xI3G9sN0yfHaZifZ8wkzZt5f5
9Oyv4n5uOgTXI/viewform
First: A Few
More Tips
For functional-style programming
Python: Comprehension
Syntaxes
 All the major data structures
are supported:
 Iterator (most common)
 List (like Iterator, but stores
result in memory)
 Set (stores unique values)
 Dictionary (stores a keyvalue association)
 All of them support
mapping and filtering.
myIter = (x.lower() for x in src)
myList = [x.lower() for x in src]
mySet = {x.lower() for x in src}
myDict = {x.lower(): "hi" for x in src}
Java: More Stream
Shortcuts
 Get the sum of the int[] array src
int sum = IntStream.of(src).sum();
 Perform a map with ternary syntax on the List<Int> grades
Stream<String> strm =
grades.stream().map(x -> (x > 65) ? "P" : "F");
 Count how many items of a List<String> strs have at least 5 chars
int n = strs.stream().filter(x -> x.length()>5).count();
C++11: Lambda Functions
 For example, to square an std::vector containing ints:
Source Start
Source End
Destination Start
std::transform(
vec.begin(), vec.end(), vec.begin(),
[](int i){ return i*i; }
);
Lambda Function
 In order to confuse people, the C++ folks changed the
names of the common functional operations.
 Map is called “std::transform”
 Filter is called “std::remove_if”
 Reduce is called “std::accumulate”
Swiss Army Knives for String
Parsing and Formatting
Regular Expressions
Printf
 A syntax for parsing an
arbitrary string format
 A syntax for formatting output
strings
 Regex syntax is standard in
most, but not all, languages
 Printf syntax is standard in most,
but not all, languages
# Python
import re
re.match(r"\w+", str)
# Python
print("%s = %6.4f" %
("pi", math.pi))
// Java
str.matches("\\w+")
// Java
System.out.format("%s = %6.4f",
"pi", Math.PI);
Time Complexity
Review
Based on Gayle Laakmann McDowell’s
Cracking the Coding Interview, 6th Edition
Chapter VI
Sort these from slow to fast.
You don’t need to know Master Method for most
interviews, but you’ve gotta know this!
Choices
Solution
 O(n2) (aka Quadratic)
 O(n!)
 O(n) (aka Linear)
 O(2n)
 O(n log n)
 O(n2)
 O(2n)
 O(n log n)
 O(1) (aka Constant)
 O(n)
 O(n!)
 O(log n)
 O(log n)
 O(1)
If you have no clue about what’s on this slide, take CSE 241.
CtCI Chapter VI Solutions
1. O(b)
8. O(n)
2. O(b)
9. O(n2)
3. O(1)
10. O(log n)
4. O(a/b)
11. O(kck)
5. O(log n)
12. O(b log b + a log b)
6. O(sqrt n)
7. O(n)
Technical
Interviews
Based on Gayle Laakmann McDowell’s
Cracking the Coding Interview, 6th Edition
Chapters I and VII
Interviews are a
standardized method for
companies to estimate how
good of an engineer you
would be.
The kinds of questions you solve in a technical
interview are not necessarily the kind that you will
actually solve at work.
They hope that there is a correlation between
being able to solve interview questions and being
a good engineer.
Technical Interview Problem
Solving Procedure
1) Listen Carefully to the problem statement
2) Draw an example on the whiteboard
3) State the Brute Force solution
4) Discuss Time Complexity
5) Try coming up with an optimized solution
6) Implement your solution on the whiteboard
7) Test your solution
Whiteboard Language
 Ask your interviewer if they have a preferred
language. If they don’t, ask them if you can use
your choice language.
 Potential Issues
 C++: make sure you don’t leak memory
 Java: to avoid writing too much, ask your interviewer if
you can use shortcut syntax
 Python: make sure to check types, or at least tell your
interviewer verbally that you would check types
 I’ve had some interviews where the interviewer was
super picky about language and style, and others
where the interviewer didn’t care.
Tips for Success
These are mine, not from the book.
 Never Ever let the interviewer get bored
 They’re a human just like you and me.
 If they lose interest or can’t follow what you’re
doing, that’s a recipe for bad marks.
 Think Out Loud!!!
 The interviewer can’t read your mind.
 If the room is quiet for 30 seconds or more, say
what’s on your mind.
 Be Confident
 You know more than you think.
How do you
come up with
the optimal
solution?
The Million-Dollar Question
Five Strategies
Based on those in CtCI and CP3
Brute Force
and Fix
Base Case
and Build
Reduce
and Relate
Simplify and
Generalize
Do It
Yourself
Brute Force and Fix
 Given a brute force solution, identify the
following parts and fix them.
1. Bottlenecks: Focus on improving the slowest step.
Improving a faster step won’t improve your
algorithm’s overall time complexity.
2. Unnecessary Work: Reduce your search space. Are
there cases you’re testing that are redundant?
3. Duplicated Work: Eliminate repeated subcomputations. Think Fibonacci.
Example 1
Given an array of distinct integer values, count
the number of pairs of integers that have
difference k. For example, given the array
{1, 7, 5, 9, 2, 12, 3}
and the difference k=2, you would find the
pairs (1,3), (3,5), (5,7), and (7,9).
Example 2
Print all positive integer solutions to the
equation
a3 + b3 = c3 + d3
subject to the constraints
0 < a < b < 1000
0 < c < d < 1000
a<c
Example 3
You want to set up an irrigation system for
your garden outside. In order to have enough
sprinklers, you bought n hose splitters at the
hardware store. Each splitter is binary: it
takes an inlet stream and produces two outlet
streams. In how many different
configurations can you arrange them?