technical interviews shane carr cse 232, september 11, 2015
TRANSCRIPT
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/1J006-xI3G9sN0yfHaZifZ8wkzZt5f59Oyv4n5uOgTXI/viewform
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 key-
value 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 ShortcutsGet 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 FunctionsFor example, to square an std::vector containing ints:
std::transform( vec.begin(), vec.end(), vec.begin(), [](int i){ return i*i; });
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”
Source Start Source End Destination Start
Lambda Function
Swiss Army Knives for String Parsing and Formatting
Regular Expressions A syntax for parsing an
arbitrary string format
Regex syntax is standard in most, but not all, languages
# Pythonimport rere.match(r"\w+", str)
// Javastr.matches("\\w+")
Printf A syntax for formatting output
strings
Printf syntax is standard in most, but not all, languages
# Pythonprint("%s = %6.4f" % ("pi", math.pi))
// JavaSystem.out.format("%s = %6.4f", "pi", Math.PI);
Time Complexity
ReviewBased on Gayle Laakmann McDowell’s
Cracking the Coding Interview, 6th EditionChapter 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 O(n2) (aka Quadratic)
O(n) (aka Linear)
O(n log n)
O(2n)
O(1) (aka Constant)
O(n!)
O(log n)
Solution O(n!)
O(2n)
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
If you have no clue about what’s on this slide, take CSE 241.
CtCI Chapter VI Solutions1. O(b)
2. O(b)
3. O(1)
4. O(a/b)
5. O(log n)
6. O(sqrt n)
7. O(n)
8. O(n)
9. O(n2)
10. O(log n)
11. O(kck)
12. O(b log b + a log b)
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 Procedure1) 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 SuccessThese 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.
Five StrategiesBased on those in CtCI and CP3
Brute Force and Fix
Base Case and Build
Simplify and
Generalize
Reduce and Relate
Do It Yourself
Brute Force and FixGiven 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 sub-computations. Think Fibonacci.
Example 1Given 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 2Print all positive integer solutions to the equation
a3 + b3 = c3 + d3
subject to the constraints
0 < a < b < 10000 < c < d < 1000a < c
Example 3You 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?