-
Notifications
You must be signed in to change notification settings - Fork 160
/
QuantitativePrimer.tex
2001 lines (1734 loc) · 83.2 KB
/
QuantitativePrimer.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[a4paper]{article}
\usepackage{qabook}
% Version number policy:
% Should be of the format V<A>.<B>.<C>
% A: Increment with major changes in layouts, large new sections added.
% B: Increment when new questions are added
% C: Increment with minor error corrections and additions
% When B is incrememted, C is reset to zero
% When A is incrememted, B and C is reset to zero
\newcommand{\docversion}{V1.2.0}
\begin{document}
%\doublespacing
%\linenumbers
\renewcommand{\thepage}{\roman{page}}
\setcounter{page}{0}
\begin{titlepage}
\titleDB
\end{titlepage}
\begin{center}
This work is licensed under a
\href{http://creativecommons.org/licenses/by-sa/4.0/}{Creative Commons Attribution-ShareAlike 4.0}
\\ International License.
\end{center}
\begin{center}
You are free to:
\end{center}
\begin{description}
\item[Share:] Copy and redistribute the material in any medium or format. In fact, you are encouraged to do so.
\item[Adapt:] Remix, transform, and build upon the material
for any purpose, even commercially.
\end{description}
\begin{center}
Under the following terms:
\end{center}
\begin{description}
\item[Attribution:] You must give appropriate credit to the original author, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
\item[Share Alike:] If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
\item[No additional restrictions:] You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
\end{description}
\begin{center}
\url{https://creativecommons.org/licenses/by-sa/4.0/legalcode}
\end{center}
\vfill
\begin{center}
For errors, comments, or suggestions, contact Dirk Bester\\
\href{mailto:bester.dirkw@gmail.com}{bester.dirkw@gmail.com}\\
\href{https://www.linkedin.com/in/dwbester}{www.linkedin.com/in/dwbester}.\\
Visit the GitHub page to ensure you are reading the latest version \\
\href{https://github.com/dwcoder/QuantitativePrimer}{github.com/dwcoder/QuantitativePrimer}.\\
\texttt{This pdf was compiled on \moddate{\jobname.tex}.} \\
\end{center}
\vfill
\noindent
Special thanks to Luke Miller, Robert Tillman, and Jonathan Hon for some clever solutions,
William Xing for catching typos and suggestions on soft questions,
Iztok Pizorn for corrections in the code,
Nita Bester for proofreading and support,
and Joshua Curk for editing.
\clearpage
\setcounter{tocdepth}{2}
\tableofcontents
\clearpage
\renewcommand{\thepage}{\arabic{page}}
\setcounter{page}{1}
\phantomsection
\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Interview preparation is the most important part of working life.
Relative to its duration, interviewing is the activity with the largest influence on future income.
It determines how you will be spending your time, who you will be spending it with, and what you will learn from this expenditure in time, effort, and money.
In addition to competence and merit, it takes confidence to convince the person on the other side of the table that you should be hired.
Confidence takes practise.
You can't lift 200kg without proper training.
You can't excel at interviews without befitting preparation.
The best way to improve your interviewing is to do interviews and learn from your mistakes.
The second best way is to learn from the mistakes of others, and ensure your preparation matches what you will encounter.
Quantitative finance is notorious for the large number of difficult interviews that precede an offer.
These interviews are laden with brainteasers and puzzles to test your abilities, and conveniently also serve to whittle down a large candidate pool.
Candidates who study interview technique increase their probability of traversing the interview gauntlet.
There are great books with sample questions and answers and candidates should work through most of these before attempting any interviews.
The classic text is \citet{HeardOnTheStreet}, and this is the one recruiters will suggest.
Another book is \citet{JoshiQA}.
I prefer it to the former since it has more in-depth discussions about the interview process, it has an entire chapter dedicated to C++ questions, and it includes references to further reading on topics encountered throughout the questions.
\citet{WilmottFAQ} supplements these by acting as a handy reference for jargon.
A suggested reading order appears in the next section.
I interviewed in London in 2014 and again in 2017,
meeting with more than 20 firms.%
\footnote{Including, but not limited to,
Barclays,
BlackRock,
BNP Paribas,
Cumulus,
G-research,
Goldman Sachs,
GSA Capital,
JP Morgan,
MAN group,
Oxford Asset Management,
Royal Bank of Canada,
Royal Bank of Scotland,
SquarePoint Capital,
Two Sigma,
UBS,
and
Winton Capital.
}
These included phone interviews, Skype interviews, online pair-programming sessions, on-site exams, take-home exams, face-to-face interviews, and multiple onsites or superdays (several interviews in one day).
The preparation books mentioned above provided great practise questions, but I feel there is more to be said on interview form.
Therefore I have prepared this text with questions I encountered, split up by interviews, to give readers an indication of exactly what to expect.
Rather than providing a large collection of questions and answers, this text focuses on the intent of the questions, a deep dive into the answers, and suggestions on communicating them during an interview.
\index{rejection, dealing with}
Interviewing is a numbers game.
Assume you will get one out of ten jobs you interview for and act accordingly, but don't go to any interviews without serious preparation.
Don't get too sentimental about any one team or role, and don't take rejection as a comment on your abilities.
During my 2014 interview spate I received an offer from a hedge fund who told me I was one of the strongest candidates in their graduate pool.
A day later I received a first-round rejection from another hedge fund who derided my abilities to do basic mathematics.
Rather than pick sides, I implore the reader to accept that it is difficult for someone to judge your ability by asking you a few brainteasers or proofs---to which they already know the answer---with only ten minutes allowed for the exercise.
\phantomsection
\addcontentsline{toc}{subsection}{How to prepare}
\subsection*{How to prepare}
The minimum amount of preparation required will depend on how much you currently do and enjoy brainteasers, coding puzzles, and statistical proofs.
Of course, you'll want to do more than the minimum.
I suggest you begin with the text you are reading now, starting with a review of the calculus mentioned in appendix \ref{ap:cribsheet}.
If you can successfully do all the questions on your first try, you are probably ready.
Read through the solutions, paying attention to how the interviewers expect you to present the answers, and in order to get an idea about how interviews progress.
If you struggle with some of the questions, work through similar questions in \citep{JoshiQA}.
Also, make sure you work through some of \citet{HeardOnTheStreet}, but allocate more time to \citet{JoshiQA}.
The former text is a classic, but it is starting to show its age---the first version was published in 1995.
The latter is more up to date and the lead author, Mark Joshi, has written plenty on quantitative-finance interviews, like
\emph{On Becoming a Quant}.\footnote{\url{http://www.markjoshi.com/downloads/advice.pdf}}
Keep \citet{WilmottFAQ} on your night table---it's excellent both as a general reference work, and as a repository of the nastier brainteasers.
For general bed-time reading---as opposed to hard-core, interview-preparatory reading---I suggest
\citet{mcneil2015quantitative}, who give a thorough overview of models used for quantitative risk management, and
\citet{andersen2009handbook}, who provide an exhaustive review of modelling financial time series.
These two are tomes---you won't be able to read them cover to cover.
Don't just skim them and say you've read them.
Pick the chapter you find most palatable in each book and read it
closely enough that you can have a short discussion about it, and sound like you know what you're talking about.
\citet{narang2013inside} presents a non-technical overview of quantitative and high-frequency trading (minus the hype), and does plenty to demystify the field.
For the hype, see
\emph{The Quants} by \citet{patterson2010quants}
or
\emph{Flash Boys} by \citet{lewis2014flash}.
Not everyone who self-identifies as a quant agrees with the contents of these, but most people in the industry have read them.
For a less flattering take, see the review of \emph{The Quants} by \citet{steinsaltz2011value} that appeared in the Notices of the American Mathematical Society.%
\footnote{\url{steinsaltz.me.uk/papers/quants_published.pdf}}
\phantomsection
\addcontentsline{toc}{subsection}{Recruiters}
\subsection*{Recruiters}
\index{recruiters}
Few resources explain how to interact with recruiters, and the ones that do tend to be written by people who have had bad experiences.
This is obviously not representative.
In my experience the following points are important.
\emph{Don't feel bad when a recruiter goes cold on you.}
Sometimes a recruiter will stop replying to your messages, especially after a failed interview.
Think about it from their perspective.
They are probably sending 10 or 20 CVs to each position.
They don't have time to get sentimental about candidates---it's nothing but a numbers game for them.
They have quotas to match and bosses to keep happy, and thus cannot always give all of their candidates equal amounts of attention.
\emph{Don't be rude to recruiters.}
Interviewing is frustrating. Especially after several interviews with several recruiters.
Each will involve a call and a get-to-know-you chat where you have to sell yourself; keep in mind that it could very well be for naught.
When the frustration hits, consider this:
you have nothing to gain from being rude.
You might actually do damage to your reputation by being rude, since the market is small and recruiters talk.
You might feel used and abused, but
you have nothing to gain from being rude to a recruiter.
Even if you are not currently job hunting and a recruiter cold calls you, be courteous.
Even if you perceive unprofessional behaviour, be polite.
\emph{Don't get sentimental.}
If things don't work out, the person who had the highest expectations is hurt the most.
You can't control the outcomes---only against exaggerated expectations.
Like counting your chickens before they hatch, it is generally a bad idea to get too excited after what felt like a successful interview, if only because it can interfere with your focus.
Like the aforementioned recruiters, play the numbers game and treat every interview like just another interview.
This is a difficult mindset to have if you really need or want a job, but it is a healthy one, especially when the time comes to negotiate.
\section{Interviews}
I made every attempt to record my interviews in 2017.
Keeping an interview journal will help you prepare for future interviews.
Some of the firms have candidates sign non-disclosure agreements, prohibiting you from sharing the questions, though most don't.
This text respects the contractual obligations I am subject to.
\citet{HeardOnTheStreet},
\citet{JoshiQA}, and
\citet{WilmottFAQ} already exist, so I will not attempt to recreate them.
Instead, I will focus not only on \emph{what} to answer, but \emph{how} to answer.
I would often use answers from these texts, only to find that interviewers were unsatisfied.
They would literally say ``Yes, but could you answer it again, this time in a different way.''
Not only were they looking for a textbook answer, they were looking for \emph{a specific} textbook answer.
I also give the duration of each interview.
This isn't the amount of time allocated to the technical questions---for those you usually have 5--15 minutes to solve each problem.
\index{questions}
\subsection{Face-to-face, 1 hour}
\begin{question}{rolltwodice}
\index{questions!dice, one larger}
Roll two dice. What is the probability that one is larger than the other?
\end{question}
\begin{question}{bivariatenormalmax}
\index{questions!E@ $E(\text{max})$ bivariate normal}
If
\begin{align*}
\begin{bmatrix}
X \\ Y
\end{bmatrix}
\sim
\operatorname{MultivariateNormal}
\left(
\begin{bmatrix}
0 \\ 0
\end{bmatrix}
,
\begin{bmatrix}
1 & \rho \\
\rho & 1 \\
\end{bmatrix}
\right)
\end{align*}
find $\operatorname{E}[\max(X,Y)]$.
\end{question}
\begin{question}{makematrixfrombunchofvectors}
\index{questions!R!make matrix from vectors}
How would you make a matrix from a bunch of vectors in R?
\end{question}
\begin{question}{pythonlistreturnlast}
\index{questions!Python!return last item}
If \verb+mylist+ is a list in Python, what is \verb+mylist[-1]+?
\end{question}
\begin{question}{cppvirtualfunctionwhy}
\index{questions!C++ virtual function}
What is a C++ virtual function, and why do we need them?
\end{question}
\clearpage
\input{answers/rolltwodice.tex}
\input{answers/bivariatenormalmax.tex}
\input{answers/makematrixfrombunchofvectors.tex}
\input{answers/pythonlistreturnlast.tex}
\input{answers/cppvirtualfunctionwhy.tex}
\clearpage
\subsection{Face-to-face, 1 hour}
\begin{question}{normalfourthmoment}
\index{questions!normal fourth moment}
Derive $\operatorname{E}(X^4)$ where $X \sim \text{Normal}\left(0, \sigma^2\right)$.
\end{question}
\begin{question}{arraymissingnumber}
\index{questions!array with missing number}
You have an unsorted array containing integers $1,2,3, \ldots, n$, but one number is missing.
Describe an algorithm to find the missing number and discuss its complexity.
\end{question}
\begin{subquestion}{arraymissingnumber:a}
Describe an algorithm assuming there are $k$ missing numbers, where $k$ is much smaller than $n$.
\end{subquestion}
\begin{subquestion}{arraymissingnumber:b}
Describe an algorithm assuming the initial array was sorted.
\end{subquestion}
\clearpage
\input{answers/normalfourthmoment.tex}
\input{answers/arraymissingnumber.tex}
\input{answers/arraymissingnumber_a.tex}
\input{answers/arraymissingnumber_b.tex}
\clearpage
\subsection{Face-to-face, 1 hour}
\begin{question}{normalestimatorssamplingdistribution}
\index{questions!normal estimators}
Suppose
$Y \sim \text{Normal}(\mu, \sigma^2)$.
Now, $10^6$ people each draw 1000 samples from this distribution.
Let $i$ denote the $i$th person.
Each person estimates the parameters of the normal distribution
$\hat{\mu}_i$
and
$\hat{\sigma}^2_i$
using their samples
$Y_1, Y_2, \ldots , Y_{1000}$.
How should they do this?
If you draw a histogram of the $10^6$ estimates of
$\hat{\mu}_i$ and $\hat{\sigma}^2_i$, what would their distributions be?
How would you prove the exact sampling distribution of $\hat{\sigma}^2_i$?
\end{question}
\begin{question}{derivativeofinverse}
\index{questions!derivative of inverse}
If we have $g'(x)$,
what can you say about $\frac{d}{dx} g^{-1}(x)$?
\end{question}
\clearpage
\input{answers/normalestimatorssamplingdistribution.tex}
\input{answers/derivativeofinverse.tex}
\clearpage
\subsection{Phone interview, 45 minutes}
\begin{question}{coin100flipsgamble}
\index{questions!coin flip gamble}
You are presented with the following gamble: you flip 100 fair coins.
If 60 or more land on heads, you win \pounds 10; you win nothing on all other outcomes.
Should you play this game for \pounds 1?
\end{question}
\begin{question}{iszerocorrelationindependent}
\index{questions!correlation and dependence}
You have $X \sim \text{Normal}(0,1)$ and $Y \sim \text{Normal}(0,1)$.
If the correlation coefficient is $\rho_{XY}=0$, are $X$ and $Y$ independent?
\end{question}
\begin{subquestion}{iszerocorrelationindependentexamples}
\index{questions!correlation and dependence}
Give some examples where $X$ and $Y$ are in fact dependent, but where the above still holds.
\end{subquestion}
\clearpage
\input{answers/coin100flipsgamble.tex}
\input{answers/iszerocorrelationindependent.tex}
\input{answers/iszerocorrelationindependentexamples.tex}
\clearpage
\subsection{Online pair-programming interview, 60 minutes}
\begin{question}{twournsallocateballs}
\index{questions!urns and balls}
You have two urns, five red balls, and five blue balls.
You can distribute the balls into the urns any way you like, but each urn must have at least one ball in it.
I will choose one urn at random ($p=0.5$) and then draw one ball from it.
If the ball is blue, you win.
How should you distribute the balls to maximise your probability of winning?
Log into this pair-programming website and use Python or C++ to solve the problem while I watch.
\end{question}
\clearpage
\input{answers/twournsallocateballs.tex}
\clearpage
\subsection{Phone interview, 45 minutes}
\begin{question}{judgescorrectverdict}
\index{questions!three judges}
There are three judges in a court, who have the following probabilities of reaching the correct verdict:
\begin{center}
\begin{tabular}{ccc}
Judge 1 & Judge 2 & Judge 3 \\
$p$ &
$p$ &
$\nicefrac{1}{2}$ \\
\end{tabular}
\end{center}
A verdict is only decided if at least two of the judges agree.
What is the probability of the court reaching the correct verdict?
\end{question}
\begin{question}{regressiontheory1}
\index{questions!regression theory}
Suppose you wanted to model $Y$ using $X$, and you decided to use the linear regression:
\[
Y = X \beta + \varepsilon
\text{.}
\]
What assumptions are being made?
How would you find $\beta$?
What tests can be done on $\beta$ afterwards?
\end{question}
\begin{question}{bayeslawdisease}
\index{questions!Bayes' law}
One percent of people in the world have a given disease,
the test for which is imperfect.
The test has an 80\% chance of showing positive if you have the disease, but if you do not have the disease,
there is a 10\% chance of showing a false positive.
What is the probability that you actually have the disease if your test results are positive?
\end{question}
\clearpage
\input{answers/judgescorrectverdict.tex}
\input{answers/regressiontheory1.tex}
\input{answers/bayeslawdisease.tex}
\clearpage
\subsection{Onsite, 5 interviews, 3 hours}
\begin{question}{derivexpowx}
\index{questions!derive $x^x$}
What is $\frac{d}{dx}x^x$?
\end{question}
\begin{question}{epiandpie}
\index{questions!E@$e$ and $\pi$}
Which is larger,
$e^\pi$
or
$\pi^e$?
\end{question}
\begin{question}{ar1processmeanandvar}
\index{questions!AR(1) process}
Given the AR(1) process
\begin{align*}
Y_t &= \alpha_0 + \alpha_1 Y_{t-1} + \varepsilon_{t} \\
& \text{where} \\
\varepsilon_{t} &\sim \text{Normal}(0,\sigma_{\varepsilon}^2)
\text{,}
\end{align*}
what are
$\E(Y_t)$
and
$\Var(Y_t)$?
\end{question}
\begin{question}{constructzerocorrelation}
\index{questions!correlation and dependence}
If $X \sim \text{Normal}(0, 1)$ and $Y$ has a distribution where:
\begin{align*}
P(Y=-1) &= \nicefrac{1}{2} \\
P(Y=1) &= \nicefrac{1}{2}
\end{align*}
what is the cumulative distribution function of $Z=XY$?
\end{question}
\begin{question}{iszerocovarianceindependent}
\index{questions!correlation and dependence}
If $\Cov(X,Y)=0$, are $X$ and $Y$ independent?
\end{question}
\begin{question}{stickbreak}
\index{questions!stick breaking}
Break a $1m$ stick in two random places.
What is the probability that the three resulting pieces form a triangle?
\end{question}
\begin{question}{pythonanagrams}
\index{questions!Python!anagrams}
Write a Python function to check whether two strings are anagrams.
Do a version with and without sorting.
Why might you want a function that can do this without sorting?
\end{question}
\begin{question}{ncralgorithm}
\index{questions!Python!N@${}_nC_{r}$}
Without using the standard library, write a function for ${}_nC_{r}$.
Do a version with and without recursion.
\end{question}
\clearpage
\input{answers/derivexpowx.tex}
\input{answers/epiandpie.tex}
\input{answers/ar1processmeanandvar.tex}
\input{answers/constructzerocorrelation.tex}
\input{answers/iszerocovarianceindependent.tex}
\input{answers/stickbreak.tex}
\input{answers/pythonanagrams.tex}
\input{answers/ncralgorithm.tex}
\clearpage
\subsection{Phone interview, 50 minutes}
\begin{question}{romeojuliet}
\index{questions!Romeo and Juliet}
Romeo and Juliet agree to meet between 08:00 and 09:00.
Each arrives at a random time in the hour and then waits 15 minutes.
What is the probability that they meet?
\end{question}
\clearpage
\input{answers/romeojuliet.tex}
\clearpage
\subsection{Onsite, 6 interviews, 6 hours}
\begin{question}{davidbeckhamparty}
\index{questions!David Beckham party}
Consider a party where there are $N$ people present.
We have a function that tests whether person $a$ knows person $b$:
\[
\text{knows}(a, b)
\text{.}
\]
The function returns true or false.
It is not necessarily symmetric:
\[
\text{knows}(a, b) \neq \text{knows}(b, a)
\text{.}
\]
For instance, I know David Beckham, but he doesn't know me.
At a party, every guest knows at least one other person, except for David Beckham, who is also at the party---everybody knows him, but he knows no one.
Numbering the people at the party from $1$ to $N$ and using the
$\text{knows}()$
function, how would you determine Beckham's number?
Now, pretend Victoria Beckham is also at the party, and that she only knows David, and he only knows her (and everyone at the party knows both of them and at least one other person).
How would you determine their numbers?
\end{question}
\begin{question}{arrayofintegersfindduplicate}
\index{questions!array find duplicate}
\index{questions!sorting}
You have an array of $N$ integers, unsorted:
\begin{align*}
[n_1, n_2, \ldots , n_{N} ]
\text{.}
\end{align*}
All the integers are unique, except two.
These are $N$ arbitrary integers---they aren't necessarily the numbers from $1$ to $N$.
How would you find the duplicate?
Give an answer that doesn't rely on sorting.
Give an answer with sorting, and discuss your favourite sorting algorithm.
\end{question}
\begin{question}{criminalsinfield}
\index{questions!murderers in a field}
You are guarding 100 murderers in a field, and you have a gun with a single bullet.
If any one of the murderers has a non-zero probability of surviving, he will attempt to escape. If a murderer is certain of death, he will not attempt an escape.
How do you stop them from escaping?
\end{question}
\clearpage
\input{answers/davidbeckhamparty.tex}
\input{answers/arrayofintegersfindduplicate.tex}
\input{answers/criminalsinfield.tex}
\clearpage
\subsection{Phone interview, 45 minutes}
\begin{question}{regressiontheory2}
\index{questions!regression theory}
What are the significance tests used for the parameters estimated in a logistic regression?
How are they different than those used for a linear regression?
\end{question}
\begin{question}{twopiecesofwood}
\index{questions!measure pieces of wood}
You have two pieces of wood of length $a$ and $b$, where $a<b$, and a measuring apparatus with a variance of $\sigma^2$, due to measurement error.
It costs \pounds 1 per use.
You only have \pounds2.
What is the best strategy to measure $a$ and $b$ with minimal variance?
\end{question}
\begin{question}{bagnsockstwored}
\index{questions!bag of socks}
A bag contains $N$ socks, some of which are black, and some of which are red.
If two random socks are picked, the probability that they are both red is $\nicefrac{1}{2}$.
What is the smallest possible value of $N$ for which this is possible?
\end{question}
\clearpage
\input{answers/regressiontheory2.tex}
\input{answers/twopiecesofwood.tex}
\input{answers/bagnsockstwored.tex}
\clearpage
\subsection{Onsite, 5 interviews, 3 hours}
\begin{question}{regressiontheory3}
\index{questions!regression theory}
What are the assumptions required for a linear regression?
What is multicollinearity, and what are its implications?
How would you measure goodness of fit?
\end{question}
\begin{question}{nameafewnonlinearmodels}
\index{questions!non-linear models}
What are some non-linear models?
\end{question}
\begin{questionwithnoanswer}
\index{questions!SQL}
Write some SQL queries.
Consider a table with the following headings:
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
Employee name & Department & Salary \\
\hline
\end{tabular}
\end{center}
\end{questionwithnoanswer}
\begin{subquestion}{sqladdaveragesalary}
Add a column to show the average department salary for each employee.
\end{subquestion}
\begin{subquestion}{sqlhighestsalary}
Write a query to return the second highest salary.
\end{subquestion}
\begin{question}{sqlfindcustomerswithchanges}
You are given the following table:
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Customer ID & Name & datestart & dateend \\
\hline
A & Jon & 1991-05-06 & 1992-05-06 \\
A & Jonathan & 1992-05-06 & 1998-10-02 \\
B & Chris & 1983-01-01 & 1997-12-16 \\
C & Sean & 1991-05-06 & 2000-05-12 \\
\hline
\end{tabular}
\end{center}
Write a query to return all the customers who have made changes to their details.
\end{question}
\clearpage
\input{answers/regressiontheory3.tex}
\input{answers/nameafewnonlinearmodels.tex}
\input{answers/sqladdaveragesalary.tex}
\input{answers/sqlhighestsalary.tex}
\input{answers/sqlfindcustomerswithchanges.tex}
\clearpage
\subsection{Face-to-face, 2 hours}
\begin{question}{regressiontheory4}
\index{questions!regression theory}
What assumptions are needed for a linear regression?
Are they the same for a logistic regression?
How would you test the significance of the parameters for a linear regression?
Would you use the same test in the case of a logistic regression?
\end{question}
\begin{questionwithnoanswer}
Answer the following questions about R.
\end{questionwithnoanswer}
\begin{subquestion}{rdatastructures}
\index{questions!R!data structures}
What are some data structures used in R?
\end{subquestion}
\begin{subquestion}{rmergetwodataframes}
\index{questions!R!merge data frames}
Describe the syntax you would use to merge two data frames.
\end{subquestion}
\begin{subquestion}{rapplyvsforloops}
\index{questions!R!L@\verb+lapply+ vs for loop}
What is the difference between \verb+lapply()+ and for-loops?
Which do you prefer?
\end{subquestion}
\begin{question}{grownup}
Here is a specific problem the team faced last year.
What model will you build to solve it, and why?
\end{question}
\clearpage
\input{answers/regressiontheory4.tex}
\input{answers/rdatastructures.tex}
\clearpage
\input{answers/rmergetwodataframes.tex}
\input{answers/rapplyvsforloops.tex}
\input{answers/grownup.tex}
\clearpage
\subsection{Video interview, 1 hour}
\begin{question}{simplerandomwalkhittingprobability}
\index{questions!simple random walk hitting probability}
You have a simple random walk,
\[
X_t = \sum_{i=1}^{t}{Z_i}
\]
where
\begin{align*}
P(Z_i = 1) &= 0.5 \\
P(Z_i = -1) &= 0.5
\text{.}
\end{align*}
If the process is currently at $k$, meaning $X_0=k$, what is the probability that it would hit $l$ before hitting $0$,
where $k < l$?
\end{question}
\clearpage
\input{answers/simplerandomwalkhittingprobability.tex}
\clearpage
\subsection{Phone interview, 1 hour}
\begin{question}{addnumbersgame}
\index{questions!add numbers game}
We are going to play a game using the integers between one and ten, inclusive.
You start, and say a number between one and ten.
Thereafter, I add a number between one and ten to your number and say the result.
Then it is your turn again.
You add a number between one and ten to the previous number I said.
We carry on, taking turns to add to the most recent number, and the first person to say 50 wins.
How much would you wager on this game?
\end{question}
\begin{question}{drop20cards}
\index{questions!drop 20 cards}
I have a deck of 52 cards.
I shuffle the deck, and drop 20 cards randomly into a shredder.
You then draw two cards from what remains.
What is the probability that they are both aces?
\end{question}
\clearpage
\input{answers/addnumbersgame.tex}
\input{answers/drop20cards.tex}
\clearpage
\subsection{Face-to-face, 1 hour}
\begin{question}{bayescoins}
\index{questions!Bayes' law and coin flips}
You have a bag with 1000 coins in it.
One of them is a double headed coin, the other 999 are fair coins.
I pick one coin from the bag at random, and flip it ten times.
It comes up heads all ten times.
What is the probability that I have selected the double headed coin?
\end{question}
\clearpage
\input{answers/bayescoins.tex}
\section{Old friends}
Two questions stood out:
I got the \emph{Air Force One} question three times in 2014, and I got the \emph{Stick Breaking} question more than three times during 2017.\footnote{I never got \emph{Air Force One} in 2017, nor did I get \emph{Stick Breaking} in 2014. Questions seem to go in and out of fashion and there is evidence of cross-pollination.}
Getting a question multiple times allows you to repeatedly sample the experience, and in doing so, learn more about the inherent uncertainty in interviews.\footnote{See question \ref{q:twopiecesofwood}.}
Like stand-up comedians who find that different crowds laugh at different parts of the same joke, different interviewers won't always approve of the same answer.
Some prioritise intuition, while others want you to grind through the mathematics, rigorously proving every step.
While both of these questions appear in the popular interview manuals---both with elegant solutions---I can report that these answers aren't sufficient to satisfy interviewers---a deeper dive is needed.
For each of these questions I will present the answers from the manuals and then show how my interviewers wanted them to be answered.
\subsection{Air Force One}
\label{q:airforceone}
\index{questions!Air Force One}
This question is sometimes called ``the drunken passenger''.
\begin{quote}
One hundred people are in line to board a plane which has exactly 100 seats.
Each passenger has a ticket assigning them to a specific seat, and the passengers board one at a time.
The first person to board is drunk, picks a random seat, and sits in it.
The remaining passengers board; if they find their assigned seat empty, they sit in it.
If they find their seat taken, they pick a random seat to sit in.
Everyone boards, and is seated.
What is the probability that the final person who boards gets to sit in their assigned seat?
\end{quote}
This was the first brainteaser I was ever asked in a live interview.
It is question 3.25 in \citet{JoshiQA}, and the question titled ``Air Force One'' in \citet{WilmottFAQ}.
Their solutions are below, produced verbatim.
\citet{JoshiQA}:
\begin{quote}
There are a number of ways to do this. Most of
these are tricky and complicated algebraically. However, if we condition on the
right event it becomes easier.
Every person who does not sit in their own seat can do three things. Sit in
seat 1, sit in seat 100 or force someone else to sit in the wrong seat.
If they sit in seat 1, then every subsequent person sits in the correct seat,
and therefore so does the last passenger.
If they sit in seat 100, then the last passenger sits in the wrong seat.
If they sit in any other seat, we are back in the same situation with fewer
passengers and seats.
Consider the last passenger who has a choice. This is a passenger below 100
who has been displaced. Since he is the last one displaced he must either sit in
seat 100 or seat 1.
The answer is therefore 1/2, since the probability of sitting in either of these
two seats is equal.
\end{quote}
Simple! You think nothing more of it and give this answer when prompted. Then the interviewer says ``I don't understand your answer, could you give a different one?''
Even though you trust and believe the simple solution, chances are that the interviewer wants to see \emph{some} mathematics.
The first two times I got this question I got stuck after giving a simple answer, not knowing how to convince the interviewer that I understood the principles behind it.
\citet{WilmottFAQ} has a similar solution, put slightly differently. (Note that he calls the confused passenger GW, who is about to board Air Force One, but cannot read and therefore ignores his ticket.)
\begin{quote}
Sounds really complicated, because of all the people
who could have sat in the last person’s seat before
their turn. Start by considering just two people, GW and
you. If GW sits in his own seat, which he will do 50\%
of the time, then you are certain to get your allocated
seat. But if he sits in your seat, again with 50\% chance,
then you are certain to not get the right seat. So a priori
result, 50\% chance. Now if there are three people, GW
either sits in his own seat or in your seat or in the
other person’s seat. The chances of him sitting in his
own seat or your seat are the same, and in the former
case you are certain to get your correct seat and in
the latter you are certain to not get it. So those two
balance out. If he sits in the other person’s seat then
it all hinges on whether the other person then sits in
GW’s seat or yours. Both equally likely, end result 50-50
again. You can build on this by induction to get to the
simple result that it is 50-50 whether or not you sit in
your allocated seat.
\end{quote}
This answer is a bit more informative, and Wilmott left the algebra as an exercise to the reader.
His suggestion to start with the case of one passenger is a good idea, and it is the first trick discussed in appendix \ref{ap:tricks}.
The notation can be tricky, so it is good to work through this beforehand so you don't struggle in a live interview.
Assume that the passengers have tickets numbered from 1 to 100, and that they board in the order of their assigned tickets.\footnote{This assumption makes the notation easier, but it doesn't affect the answer. You can renumber tickets any way we want, the only thing that matters is the order in which they board and how many seats are left for each passenger.}
Note that any person who gets on the plane and finds his seat occupied \emph{becomes the drunkard.}
This means that if there are 100 seats, and the drunk passenger sits in seat 50, people 2--49 will simply sit in their assigned seats.
When person 50 finds his seat occupied, he becomes the drunkard and the problem still exists, but with 51 people instead of 100. The open seats will be
$1, 51,52,\ldots,99,100$
and passenger 50 can choose any of them randomly. For simplicity, relabel seat $1$ and call it seat $50$, and pretend it is the allocated seat for passenger 50 (since it is the ``correct'' choice that makes everyone else get their assigned seats).
This allows the problem to be solved using induction.
Let $p_n$ be the probability that the last person sits in their assigned seat when there are $n$ seats on the plane.
You want to determine $p_{100}$, since it is what the question requires.
Let $A$ be the event that the last person sits in their own seat, and $B_i$ be the seat number chosen by a person who gets on when there are $i$ seats left.
It follows a discrete uniform distribution from $1$ to $i$, so $P(B_i = k) = \frac{1}{i}$ since there are $i$ seats and the person randomly chooses one.
Start with the trivial case of $n=1$ where
\[
p_1 = 1
\text{,}
\]
which doesn't help.
To keep the notation manageable, assume the $n$th person is assigned to seat $n$.
Now consider the case with two people,
\begin{align*}
p_2 = P(A|B_2 = 1)P(B_2=1) + P(A|B_2 = 2)P(B_2=2)
\text{.}
\end{align*}
Here, think of $P(A|B_2 = 1)$ as ``the probability that the last person sits in his own seat if person just before him sits in their own seat.''
Similarly, think of $P(A|B_2 = 2)$ as ``the probability that the last person sits in his own seat if person just before him actually took it by mistake'', which is zero:
\begin{align*}
p_2 &= (1)\frac{1}{2} + (0)\frac{1}{2} \\
&= \frac{1}{2}
\end{align*}
That's a lot of notation to arrive at something trivial.
The picture starts to emerge when $n=3$.
It helps to think about it in words first.
When there are three people, the first one to board the plane can do one of three things, as highlighted in the following table:
\begin{center}
\begin{tabular}{lclc}
\hline
Action & $P(\text{Action})$ & Consequence & $P(A|\text{Action})$ \\
& $P(B_3 = k)$ & & \\
\hline
He sits in seat 1 (his own)& $\nicefrac{1}{3}$ & Crisis averted & 1\\
He sits in seat 2 & $\nicefrac{1}{3}$ & The second person becomes the drunkard & $p_2$\\
He sits in seat 3 & $\nicefrac{1}{3}$ & The last person is doomed & 0\\
\hline
\end{tabular}
\end{center}
Recall that $A$ is the event where the last person to board the plane gets their allocated seat.
Express this in the confusing notation:
\begin{align*}
p_3
&=
\phantom{{} + {}} P(A|B_3 = 1)P(B_3=1) \\
&\phantom{{} = {}} + P(A|B_3 = 2)P(B_3=2) \\
&\phantom{{} = {}} + P(A|B_3 = 3)P(B_3=3)
\\
&=
(1) \frac{1}{3} +
(p_2) \frac{1}{3} +
(0) \frac{1}{3} \\
& =\frac{1}{3}\left(1 + \frac{1}{2} + 0\right) \\
& =\frac{1}{3}\left(\frac{3}{2}\right) \\
& =\frac{1}{2}
\text{.}
\end{align*}
Doing it for $n=4$ reveals the pattern.
The first person to board the plane has the following choices:
\begin{center}
\begin{tabular}{lclc}
\hline
Action & $P(\text{Action})$ & Consequence & $P(A|\text{Action})$ \\
& $P(B_4 = k)$ & & \\
\hline
He sits in seat 1 (his own)& $\nicefrac{1}{4}$ & Crisis averted & 1\\
He sits in seat 2 & $\nicefrac{1}{4}$ & The second person becomes the drunkard & $p_3$\\
He sits in seat 3 & $\nicefrac{1}{4}$ & The third person becomes the drunkard & $p_2$\\
He sits in seat 4 & $\nicefrac{1}{4}$ & The last person is doomed & 0\\
\hline
\end{tabular}
\end{center}
\begin{align*}
p_4
&= \phantom{{} + {}} P(A|B_4 = 1)P(B_4=1) \\
&\phantom{{} = {}} + P(A|B_4 = 2)P(B_4=2) \\
&\phantom{{} = {}} + P(A|B_4 = 3)P(B_4=3) \\
&\phantom{{} = {}} + P(A|B_4 = 4)P(B_4=4)
\\
& =
(1) \frac{1}{4} +
(p_3) \frac{1}{4} +
(p_2) \frac{1}{4} +
(0) \frac{1}{4} \\
& =\frac{1}{4}\left(1 + \frac{1}{2} + \frac{1}{2} + 0\right) \\
& =\frac{1}{4}\left(\frac{4}{2}\right) \\
& =\frac{1}{2}
\text{.}
\end{align*}
Doing it for $n$ people, the first person to board the plane can choose from the following actions:
\begin{center}
\begin{tabular}{lclc}
\hline
Action & $P(\text{Action})$ & Consequence & $P(A|\text{Action})$ \\
& $P(B_n = k)$ & & \\
\hline
He sits in seat 1 (his own)& $\nicefrac{1}{n}$ & Crisis averted & 1\\
He sits in seat 2 & $\nicefrac{1}{n}$ & The second person becomes the drunkard & $p_{n-1}$\\
He sits in seat 3 & $\nicefrac{1}{n}$ & The third person becomes the drunkard & $p_{n-2}$\\
He sits in seat 4 & $\nicefrac{1}{n}$ & The fourth person becomes the drunkard & $p_{n-3}$\\
\vdots & \vdots & \vdots & \vdots \\
He sits in seat $i$ & $\nicefrac{1}{n}$ & The $i$th person becomes the drunkard & $p_{n-i+1}$\\
\vdots & \vdots & \vdots & \vdots \\
He sits in seat $n-3$ & $\nicefrac{1}{n}$ & The fourth-last person becomes the drunkard & $p_{4}$\\
He sits in seat $n-2$ & $\nicefrac{1}{n}$ & The third-last person becomes the drunkard & $p_{3}$\\
He sits in seat $n-1$ & $\nicefrac{1}{n}$ & The second-last person becomes the drunkard & $p_{2}$\\
He sits in seat $n $ & $\nicefrac{1}{n}$ & The last person is doomed & 0\\
\hline
% & & & \\
\end{tabular}
\end{center}
\begin{align*}
p_n