You are on page 1of 30

Examen WorldQuant

Alberto Hernández-Espinosa
March 12, 2019

Abstract
This is guide to prepare quantitative researcher test

1 Matrix chain multiplication


You have a function F that computes the product of matrices. If X is MxN
matrix and Y is NxK matrix then F(X,Y) is going to output matrix product
X*Y and take MxNxK nanoseconds. You need to calculate a product of five
matrices, P=A*B*C*D*E. The dimensions of the matrices are as follows: A
is 2x100, B is 100x10, C is 10x10, D is 10x100, and E is 100x2. How many
nanoseconds does it take to calculate P in the fastest way possible using only
function F?

2 Ray reflection
There is a unit cube with internal mirror faces. A ray is emitted into
the cube from one vertex, reflects off four faces (without touching
vertices or edges), and stops at the opposite vertex from which it
started.
What is the minimum possible distance the ray travels?
Suppose to watch the cube "from above", so that emitting vertex (red in
diagram below) and receiving vertex are opposite vertices of a unit square. We
can reflect this square about its sides, so that the light path is a straight line in
this lattice (see Figure 1). I represented in the diagram the shortest paths from
the red point to one of the black points (which are reflections of the receiving
vertex). Each horizontal reflection is represented by the path crossing one of
the lines.
The blue path is the shortest one and has no reflections: that means the four
reflections occur along the vertical. This path is identical with the black paths,
which have four horizontal reflections and no vertical one. The other paths must
be discarded, because they either have too many reflections (red paths) or hit
a vertex/edge (violet and green paths). The case of the green path is not so
obvious, but to make four reflections it has to include two vertical reflections
and the only way is to travel a unit vertical distance at each step.

1
Figure 1: Reflection

2
Figure 2: Reflection 3D

The only allowed pathpis represented in 3D in√Figure 2. It is made of five


segments, each of length 12 + (1/5)2 + (1/5)2 = 527 . The total length is then

27

3 Determinants and uniform distribution


The three distinct entries of a 2 x 2 symmetric matrix are drawn from
the uniform distribution [-60, 60]. What is the expected determinant
of the matrix?
Let  
A B
m=
B C
where A, B, C are assumed U [−60, 60] and independent.
By linearity of mathematical expectation, and independence of A, C:

E (det (M )) = E AC − B 2 = E (A) E (C) − E B 2


 

Where E (A) E (C) = 0

3
Using the fact that

V (B) = E B 2 − E B 2 = E B 2
  

and the formula for the variance of a uniform distribution that can be found
in (https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)),
1 2 1 2 1 2
E (det (M )) = − (b − a) = − (60 − (−60)) = − (120) = −1200
12 12 12
Further Explanation:
In probability theory and statistics, the continuous uniform distribution or
rectangular distribution is a family of symmetric probability distributions such
that for each member of the family, all intervals of the same length on the dis-
tribution’s support are equally probable. The support is defined by the two
parameters, a and b, which are its minimum and maximum values. The dis-
tribution is often abbreviated U(a,b). It is the maximum entropy probability
distribution for a random variable X under no constraint other than that it is
contained in the distribution’s support.

4 Clock angles
How many times a day do the hour and minute hands of an analog clock form
a right angle?
Minutes hand

360° = 60min

360
1min = = 6°
60
This last means that each minute, minutes hands advance 6 degrees
Then, to express degrees as a function of time t, in minutes

6t = degrees

Hours hand

360° = 12hr

Then

1hr = 30°

60min = 30°

30
1min = = 0.5°
60

4
Figure 3: Uniform distribution (continuous) [Source:
https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)]

5
Then

0.5t = degrees

Then, if we are looking when minutes and hours hand form a right angle,
the next makes sense:

6t − 0.5t = 90

Then

t(5.5) = 90

90 ∼
t= = 16.36min
5.5
This last means that each a little more than 16 min, clock’s hands form a
right angle. Then, for a day, this is to say, 360 ∗ 2 = 720°
720
90
 = 44
5.5

5 Code colors
If WHITE=000, RED-101, BLUE-110, and PURPLE-100, then what
three-digit string corresponds to YELLOW?
It’s a binary RGB encoding question, with values inverted (a color channel
is on when the corresponding bit is zero), and a non-standard bit order.
“Red = 101” tells you that the red bit is the one in the second position.
“Blue = 110” tells you that the blue bit is the one in the third position.
“Purple=100” confirms that you are doing additive synthesis, i.e., R + B =
Purple.
You can assume that the first bit represents green.
Now the three bits represent “[not G][not R][not B]”.
You want yellow, which is green + red.
Its value in the encoding we just decoded is 001.

6 Apples and probability


There are 4 green and 50 red apples in a basket. They are removed
one-by-one, without replacement, until all 4 green ones are extracted.
What is the expected number of apples that will be left in the basket?
So we’re looking for an average over all possible orders to pull the apples
out in. (We’ll include the ones after the last green one goes.) Here’s one way
to choose the order; I hope it’s clear that it generates all orders with equal
probability.

6
First, decide what order to take the green ones in.
Then, successively for each red apple, decide its position relative to the
apples already placed.
Note that each red apple is then equally likely to lie in each of the five regions
"before first green apple", "between first and second green apples", ..., "after
fourth green apple".
This means that for a given red apple Pr(this apple is still there after the
last green one is taken) = 1/5.
So the expected number of red apples left after the last green one is taken
is 50×1/5 = 10.

7 Shape ratio
Given two assets that have expected excess returns of 7 and 4, and
given their expected covariance matrix: {1,1}{1,2} What is the max-
imum expected Sharpe ratio that you can achieve by combining the
two assets into a portfolio?
You can do by calculus also:
Take down all the definitions and let wa = w and wb = 1 − w
Then √
V ar (P ) = 1 − w2
σP = 1 + w 2
Rp = 7w + 4(1 − w) = 3w + 4
Now
Sharpe Ratio S = √3w+4
1+w2
If you take dw
dS
and set it to 0,
and find the above derivative using quotient rule, you will get
w =0.75 and hence 1−w =0.25
Thus wa =0.75 and wb =0.25, Substitute back in S you will get the maximum
Sharpe Ratio to be equal to 5.00

8 On pederastians
A pedestrian starts walking from town A to town B. At the same time,
another pedestrian starts walking from town B to town A. They pass
each other at noon and continue on their paths. One of them arrives
at 4 PM, the other at 9 PM. How many hours had each walked before
passing each other?
Let the distance between A and B be x, the faster pedestrian’s speed s and
the slower pedestrian’s speed t. From their meeting point C at 12 pm, the
distance to A is 4s and the distance to B is 9t. Therefore, we get that 4s+9t=x.
Hence, the distance walked by the faster pedestrian before meeting the other
one is 9t and he does this in the same time that the other pedestrian does the
distance 4s. So, 4s
t = s , so 4s = 9t . Now, on simplifying, s = 2 , hence we
9t 2 2 3t

7
Figure 4: Shape Ration information

can find the original time now, by doing distance by time: 9t3t = 6. Hence, each
2
walked for six hours before the meeting.
Verifying the answer:
Both start out at six am. One walks for ten hours, one for fifteen hours,
so the speeds are 10x
and 15
x
. Hence, the meeting point should be six hours
afterwards for both, which is true because 6 10
x x
+ 6 15 = 1. Hence, both walk for
6 hours is correct.

9 About liars
Seven people are in an argument, but potentially some or all of them
are liars. They give the following statements:

1. Bob: "No one lies."


2. Jennifer: "No one tells the truth."
3. Conrad.: "Jennifer is not a liar."

4. Tom: "Conrad and Sherry always lie at the same time."


5. Sherry: "Danny never lies."
6. Danny: "Sherry is a liar."
7. Adam: "Danny sometimes lies.”

8. How many of them are lying?

8
Figure 5: Shortest path with 19 segments

Reasoning
Bob says “None lies”, this is true only if all other statements are true.
But from (2) and (3), this is a contradiction, then at least one is not true.
Then Bob is a liar, because at least one is not saying the true due above
contradiction.
Jenny says “none tells the true” = “all are liars”, that is the opposite than
Bob, then, also by contradiction Jenny is also a liar.
In consequence Conrad is also a liar, because we found Jenny is a liar.
Tom says that Conrad and Sherry are liars at the same time. As Conrad
was found to be a liar, then Sherry is a liar and Tom is saying true.
Danny says that Sherry is a liar, then Dany is saying the true, this time at
least, and Adam confirms last statement. Then Adam is saying true.
Finally, liars: Bob, Jenny, Conrad, Sherryl.
Not liars: Danny, Tom and Adam

10 Shortest path
A city is composed of three parallel east-west streets and four parallel north-
south streets: Note there are 12 intersections and 17 street segments. A police-
man needs to visit every street segment, but he wants to take the shortest path.
The policeman can start at any intersection, and he can only traverse streets,
going from one intersection to another. How many street segments are there in
the shortest path that visits each street segment at least once?
Euler said that
1. Only if all vertex are even degree, Eulerian path is possible, this is, cross
one side once.

9
2. If graph has more than two odd degree vertex, Eurelian path is not pos-
sible.

11 On variance
What is the maximal possible variance of a random variable taking
values in the interval [0, 10]?
(see figure 6)
The general problem is:
Let x be a vector of n numbers in the range [0, p], where p is a positive real
number.
What’s is the maximum of the variance function of this n numbers?
The variance of the vector x is given by:
n
1X
var(x) = (xi − x)2
n i=1

where the mean x is given by


n
1X
x= xi
n i=1

12 Correlation values
Assume there are three random variables X,Y,Z. All pairwise corre-
lations are equal: corr(X,Y) = corr(X,Z) = corr(Y,Z) = r. What is
the range of possible values of r?
Hint: There exist random variables X1 , X2 and X3 with pairwise correlations
ρ12 , ρ13 and ρ23 if and only if the matrix
 
1 ρ12 ρ13
 ρ12 1 ρ23 
ρ13 ρ23 1

is positive semidefinite. We get that there exist random variables X1 , X2 and


X3 with all pairwise correlations equal to to ρ if and only if the matrix
 
1 ρ ρ
 ρ 1 ρ 
ρ ρ 1

is positive semidefinite (see below for the explanation). This matrix is posi-
tive semidefinite precisely when ρ ∈ [−1/2, 1]
Note that there existhd random i variables X1 ...Xd s.t, corr(Xi, Xj ) = ρ (for
i 6= j) if and only if ρ ∈ − 1−d
1
,1

10
Figure 6: response

See figure 7 for explanation, but here another explanation:


There is another interpretation of the problem: you may approach it geo-
metrically. It will be much easier this way. It is a well-known fact, that you
can take correlations as cosines. And random variables are vectors in 3D space.
Then the question is just how to construct three vectors with same pairwise
angles between them. What angles can it be?
There is a theorem, which is not so hard to prove, that such angles cannot
exceed 120 degrees. So, then cos i.e correlation is greater, than -1/2.
You can read more about geometrical interpretation of probability in the
book The Geometry of Multivariate Statistics by Thomas Wickens

13 On flips
Find the expected value of the random variable X that denotes the
number of coin flips needed to get all of the same side (all heads or
all tails) when throwing three fair coins at once.
See figure 8
You have three fair coins. You repetitively flip all coins together. After each
round you remove the coins that show tails. What is the expected number of
rounds until you have no coins left to flip?
for 3 coins there are 8 possible results, if 1= head and 0 = tail:

11
Figure 7: explanation on correlation values

Figure 8: solution 1

12
000
100
010
110
001
101
011
111
Where it is less probable to have 3 or 0 tails (1/8 each), then it is equally
probable to obtain 1 or 2 tails (3/8 each) at first toss. Suppose we get one tail
(the worst of the cases), then we remains with 2 coins:
00
10
01
11
Where is 1/2 probable to get one tail than 2 or 0, this is my second toss.
Suppose I get 1 tail only, then I remains with one single coin
For one coin is equally probable head or toss, suppose the worst of the cases
I get a head, then I repeat the experiment, then I had executed at least 4 tosses.

14 On geometry
Triangle ABC has sides of length 45, 60, and 75. Place a point D ran-
domly and uniformly inside the triangle. What is the expected value
of the sum of perpendicular distances from point D to the triangle’s
three sides?
I think 47 is right, this is how I proved it.
The lengths are 45, 60 and 75. For clarity sake lets take 3,4 and 5 and then
we will scale our answer by 15.
Let the coordinate of the triangle be (0,0) (3,0) and (0,4).
Equation of Hypotenuses becomes (x/3 + y/4 -1 = 0).
Now any point (x,y) in the triangle would have perpendicular distance from
side as
x + y + (12/5 - 0.8x - 0.6x) = 2.4 + 0.2x + 0.4y
distance from x axis + distance from y axis + distance from Hypotenuses
The answer would be expected value of above
2.4 + 0.2[x] + 0.4[y]
[x] in triangle = x coordinate of centroid = 1
[y] = y coordinate of centroid = 4/3
substituting the values answer scaling to 15
(2.4 + 0.2 + 1.6/3)15 = 39 + 8 =47

13
15 On physics
Two tubes (infinite right circular cylinders) A and B of equal radio 3 cross at
the angle 90 degrees (i.e. their axial lines cross at that angle). What is the
volume of the intersection of A and B?
You have a biased coin in which the probability of getting a head
is 51%. Create an event out of this coin which has a probability of
25%.
No answer
There is a calculator in which all digits (0 to 9) and the basic
arithmetic operators (+, -, *, /) are disabled. However other scientific
functions are operational like exp, log, sin, cos, arc tan, etc. The
calculator currently displays a 0. Convert this first to 2 and then to
3.
Here we imagine a 1x1 square cut in half. The diagonal should have a length
of sqrt(2).
We use cosecant, and arctangent.
exp(0) = 1
csc(atan(1)) = sqrt(2)
sqrt(2)^2 = 2
And here we imagine line bisecting an equilateral triangle. We use cotangent
and arcosecant
cot(acsc(2)) = sqrt(3) sqrt(3)^2 = 3
There are five points in an equilateral triangle of side length 1.
Prove that there are two points with distance less than 1/2.
this can’t be proven. you can fit up to 6 points on that triangle, all with
a minimum distance equal to 1/2, but not less than 1/2. Take the equilateral
triangle (length 1), and make a smaller equilateral triangle by connecting the
midpoints of each side (length 1/2). you can prove through side-angle-side
symmetry and midpoints that the central triangle is equilateral then put points
on all triangle vertices. Minimum distance is equal to 1/2, not less than it.
FURTHER EXPLANATION
Split up the equilateral triangle into 4 little equilateral triangles of side1/2as
shown in figure 10:
It should be clear that any two points within one of the smaller 4 triangles
are within distance1/2. Since we have 5 points in the bigger triangle, somet
wo of them will necessarily be in the same small triangle – hence, that pair of
points will have distance at most 1/2, and we’re done.
Where in the earth or a sphere, you can return to the same place
once you walk 1 mile to south, 1 mile to east and 1 mile to north?
If you started at the North pole you would end up at the North pole.
If you start south of the equator you end up a more than a mile from your
starting point, north of the equator less than a mile from the start. If you start
half a mile north of the equator you end up exactly a mile west of your starting
point.
The figure 7 only if you are not a flat earther!

14
Figure 9: intersected tubes

15
Figure 10: EQUILATERAL TRIANGLE

Bob walked for 5 hours. First he followed a horizontal road, then


walked up the mountain and finally returned to the starting point
using same route. Bob’s speed on horizontal road was 4 km/h, up
the mountain 3 km/h, and down the mountain 6 km/h. How many
kilometers did Bob walk?
Since the man walked up a hill and then came back down the same hill, we
may assume that he followed the same path both times, thus he traveled the
same distance both times. We can then calculate the man’s average rate while
climbing the hill and descending from the hill. In order to use the distance
formula d=rt, we need to know the total time it took for the man to walk up
and down the hill. The time it took the man to walk up the hill would be
tu=d/3 (where d is the distance up the hill) and the time it took the man to
walk down the hill would be td=d/6. The total distance the man travels over
this period of time is 2d. Now, we know distance and time and can find the
rate.
2d = [(d/3 + (d/6)]r
2d/[(d/3 + (d/6)] = r
2/[(1/3 + (1/6)] = r
12/3 = 4 = r
Therefore, the man’s average rate up and down the hill was 4 miles per hour.
The man travels on level ground to the hill at 4 miles per hour, goes up and
down the hill at 4 miles per hour and then travels back home on level ground at
4 miles per hour. Thus, each segment of his trip covers the same distance since
he is traveling at the same rate on all three segments.

16
Figure 11: Walking around the earth

17
However, he spends 2/3 of his time on level ground and 1/3 of his time on
the hill. Now, if we let d be the total distance the man travels, we have an
equation that looks like this:
d = (2/3)(5 hours)(4 miles/hour) + (1/3)(5 hours)(4 miles/hour)
d = 40/3 + 20/3
d = 60/3 or d = 20 miles
Therefore, the man traveled a total distance of 20 miles during his journey.
How many integers n such that n^n is a perfect square are there
in range [100;400] ?
x = (int)sqrt(n2) - (int)sqrt(n1);
For example, let b = 24, a = 8.
floor(sqrt(b)) = 4, ceil(sqrt(a)) = 3.
And number of squares is 4 - 3 + 1 = 2.
The two numbers are 9 and 16.
Minimize the number of comparisons for finding minimum and
maximum of a given set of numbers.

1. Pick 2 elements(a, b), compare them. (say a > b)


2. Update min by comparing (min, b)
3. Update max by comparing (max, a)
This way you would do 3 comparisons for 2 elements, amounting to 3N/2 total
comparisons for N elements.
Divide array of numbers from 1 to 100 into 2 groups which have
the same sum.
I can take first and the last, the second and one before last... in this way:
[1, 2, 3, 4, 5, 6, 7, ..., n − 5, n − 4, n − 3, n − 2, n − 2, n]
n+1
(n − 2) + 2 = n + 1
(n − 3) + 3 = n + 1
In general I take first elementos and I put it in a set, the second in the second
alternally. Both sets will sum the same
Give you a fair die, how to create a event with probability 1/7
Roll a die twice, there would be 36 pairs. Take 35 of them as the base and
select 7 as your goal. The probability of getting your goal is 1/7.
also
If we roll 2 dice, we have 36 possible outcomes. If we discard 1 outcome
(chose to re-roll the 2 dice if we get it), then we have a space of 35 outcomes.
We need 1/7 of 35, so pick 5 outcomes of the 35. You have a 1/7 chance of
hitting any of the 5 outcomes.
Let’s play a game: There are 3 white balls and 4 black balls in a
black box. If you get a white ball out your payoff +1, and -1 with a
black ball. You can stop anytime and repeat the game infinite times.
Will you play the game?
Yes.

18
A simple strategy is as follows: if your first pick is a white ball, stop playing
(prob. 3/7, pay off +1);
if your first pick is black ball, continue picking a second ball, if the second
ball is white, then stop playing (prob. 2/7, pay off 0); if the second ball is black,
just keep picking all the balls in the jar (prob. 2/7, pay off -1).
With this, the expected pay off is +1/7 for each game.
The above answer assumes we play the game without replacement. Here’s
my attempt to work it out more clearly:
if we pick W at 1st, stop playing. Thus our expected reward from W on 1st
draw is 3/7.
If we pick B, our expected reward is 4/7 * (-1 + E(3W, 3B)), where E(3W,
3B) is the expected reward for playing the game under whatever "optimal strat-
egy" there is.
E(3W, 3B) under the proposed strategy has 2 parts:
1) if we pick W, stop. this gives E[(3W, 3B) and pick on 2nd draw W] = 1.
2) If we pick black, we lose 1, but we now have 3W and 2B in the box, so
playing to the end gives us a guaranteed reward of at least +1 (if we pick more
than 1 W in a row from the remaining (3W, 2B), stop immediately for more
reward, but ill assume for now that we only get the min reward).
Thus E[(3W, 3B) and pick on 2nd draw B] = -1 + 1 = 0. Hence E(3W, 3B)
= 1/2 (1) + 1/2(-1 + 1)= 1/2 (1) + 1/2(0) = 1/2. Thus our total expected
winnings for the game are 3/7 + 4/7(-1 + 1/2) which is amazingly 1/7 > 0!
I was surprised because if the game is played *with* replacement, you cannot
win, but apparently playing without means you can!
Let’s play another game: There are 100 balls, you and I pick the
balls in turn, for each round you can pick 2^k balls(k=0,1,2,...). Do
you have a strategy to win?
if the number of balls is 3k you always win if you are the 2nd to move. ->
choose to move first and pick 1.
Assuming whoever reaches 100th ball is the loser: Allow the other person
to pick first. If they pick 2^k, you have to pick 2^(k+1). If 2^k is the largest
element, then pick 2^(k-1). Example: They pick 1, you pick 2 making the sum
3, they pick 32 making the sum 35, you pick 64 making the sum 99. They pick
1 they loose.
Further explanation on the answer above: If you can leave 3m+1 balls (e.g.
1, 4, 7, 10, ... ), you win. Since we have 100 = 3*33+1 balls, let the other
person pick first. Then you can always leave 3m+1 balls because if he/she picks
2^k = (3-1)^k = 3n+(-1)^k, then 3m’+1+(-1)^(k+1) balls are left so you peak
2^l, where l = k+1 (mod 2). e.g. A peaks 2^3= 8. B peaks 2^6=64 (28 left).
A peaks 2^0=1. B peaks 2^3=8 (19 left). A peaks 2^3=8. B peaks 2^2=4 (7
left), etc.
Two boats are crossing the river from opposite sides. When they
first meet, they are 720 feet from the near shore. When they reach
the opposite shores, they stop for 10 minutes and cross the river
again, but this time they meet 400 ft from the far shore. How wide
is the river?

19
Let the width of the river be w.
Let the speed of the first boat be f.
Let the speed of the second boat be s.
Time of first meeting is 720/f = (w - 720)/s
f reaches far shore at w/f
waits 10 minutes and heads back
s reaches near shore at w/s
waits 10 minutes and heads back
time of second meeting is w/f + 10 + 400/f = w/s + 10 + (w-400)/s
The two 10 minute delays cancel each other out.
720s = fw - 720f
f(w-720) = 720 s
f/s = 720/(w-720)
w/f + 10 + 400/f = w/s + 10 + (w-400)/s
ws + 10fs + 400s = wf + 10fs + wf - 400f
s (400 + w) = f (2w - 400)
f/s = (400 + w)/(2w - 400)
720/(w-720) = (400 + w)/(2w - 400)
720 (2w - 400) = (400 + w) (w - 720)
1440 w - 288000 = 400w - 720w + w^2 - 288000
w^2 - 1760
w = 0 w = 1760
- - - - That’s the answer - - - -
here’s the full story.
The 10 minute delay is irrelevant, since both of them take it, so we’ll ignore
it. We can also use any absolute speeds we want, since we don’t have any specific
time information. It’s just the relative speeds that matter.
The ratio of speeds of the two boats is 9 : 13. The faster boat is the one
coming from the far shore. Let’s use 104 feet per minute and 72 feet per minute.
Then the first meeting takes place after 10 minutes, 720 feet from the near
shore and 1040 feet from the far shore. The faster boat reaches the near shore
after 16.92 minutes. The slower boat reaches the far shore after 24.44 minutes.
Ignoring the 10 minute delay, the fast boat turns around and travels 1360
feet in 13.08 minutes, total time elapsed = 16.92 + 13.08 = 30 minutes, the
slow boat turns around and travels 400 feet in 5.56 minutes, total time elapsed
= 24.44 + 5.56 = 30 minutes!
Increasing or decreasing the absolute speeds won’t matter, except for the
actual numbers of minutes. All the ratios will be preserved.
Nine boys and seven girls are seated randomly around a circular
table with 16 seats. Find the expected number of girl-boy neighbors.
For example, in the seating below there are four such pairs. GBBBBB
G B G B GGGBBG
Consider an arbitrary boy. The probability that a girl is sitting on his right
is 7/15, there are only 9 boys, that is, the expectation of the number of ’right’
MD pairs is 63/15, the same number of ’left’ DM pairs, just 126/15 or 8.4
FURTHER EXPLANATION

20
Figure 12: Boats problem

Let’s just simplify the task to show the fallacy of such logic. Suppose we
do not have 9 boys, but 3. And girls are not 7, but 2. Only 5 people. The
number of their permutations at the table = 5! / (2! 3!) = 10. We will write
out all the tuples, coding the boys 0, girls - 1, and assuming that the last digit
of each tuple joins the first one in the cycle, we indicate the number of pairs of
neighbors next to each tuple:
a.00011 - 2
b.00101 - 4
c.00110 - 2
d.01001 - 4
e.01010 - 4
f.01100 - 2
g.10001 - 2
h.10010 - 4
i.10100 - 4
j.11000 - 2
Even if we consider the tuples {a, c, f, j, g}, {b, e, i, d, h} to be equivalent,
since we are talking about cycles - it does not matter, the result will be the
same: on average - 3.
And following your logic: for each boy, the probability that the girl on the
right is 2/5. Boys - 3, so mat. waiting for the right couples - 6/5. The same -
left. Those. only 12/5 = 2.4
No, for each boy the probability is that the girl is 2/4 on the right. In
general n−1
g
. As a result, the expectation will be just 3, as it should be.
Oh yes. It turns out, I not only did not understand the logic, but also with
the arithmetic of the problem.
Actually, I was embarrassed in this decision by the fact that the probability
of the "girl on the right" event is the same as that of the "girl on the left",

21
although the realization of one of these events obviously affects the probability
of the second. But most importantly, why separately consider the neighbors
on the right and left, if the probability of "just a neighborhood" will be the
same? But probably all these "but" are exactly leveled by the circumstance of
the circular placement of children at the table, it remains only to feel it.
At a round table in random order seated 8 men and 6 women.
What is the expected value of next sitting opposite-sex couples?
Following problems could help to solve this problem
There is a round table with 16 seats. 8 men and 8 women are
going to sit at this table. What is the probability of the 16 seats being
occupied so that none of the women sit next to another woman?
Re your query about the famous (n−1)! formula, I have a few points to
make:
Here we want the probability, so it doesn’t really matter whether we take
the seats to be unnumbered (which we normally do unless it is explicitly stated
otherwise) or take them as numbered.
Taking them as numbered makes the computations symmetrical here, but
we must (and do) get the same answer if we treat them as unnumbered.
Seat the women in 7!
ways, and the men in the 8 spaces in between in 8! ways, to get P r = 7!8! 15!
FURTHER EXPLANATION
I did:
Total combinations = 16!
Total combinations in which women are not sitting next to each other:
8∗8∗7∗7∗6∗6∗5∗5∗4∗4∗3∗3∗2∗2=8!8!
So the answer is:
8!8!16!
However, my book says the solution is
2∗8!∗8!16!
Why is this? What did I do wrong?
The extra factor of two comes from which seats the women actually sit on.
For example, if you number the seats clockwise from 1 to 16, your solution gives
all cases for which the women sit on odd seats and the men sit on even seats.
But the other situation is where women sit on even seats and men sit on odd
seats, so there are actually twice as many solutions
There are 9 boys and 7 girls sitting around a table. If a boy
sits next to a girl we call it a switch. Example: In BBBBBGGG-
BBGBGGG there are 6 switches. What is the expected number of
switches?
Give the boys a number. For boy i let Gi denote the number of girls that
sit next to him.
Then you are searching for: E (G1 + ... + G9 ) = EG1 + ... + EG9 = 9EG1
Let Alex (the boy having number 1) take place.
The probability that Alex will get 2 boys as neighbor is 15 14 = 15 .
8 7 4

The probability that Alex will get 2 girls as neighbor is 15 14 = 15 .


7 6 3

The probability that Alex will get a girl and a boy as neighbor is 2 15 14 = 15 .
8 7 8

22
Figure 13: Sorting algoritms

So EG1 = 0 15 4 3
+ 2 15 8
+ 1 15 = 14
15
We come to an expectation of 9 14 15 = 5 switches.
42

Four women and four men are assigned seats at random at a round
table. What is the probability that no two persons of the same sex
will be sitting next to each other?
Suppose you are one of them, find a seat for yourself, you can sit anywhere
as you like. And then ask other people to seat down by the law. It’s easy to
see:
P = 3!4!
7! = 35
1

What is the complexity of the Quick Sort algorithm?


What is the meaning of mutable and immutable variable.
Objects of built-in types like (int, float, bool, str, tuple, unicode) are im-
mutable. Objects of built-in types like (list, set, dict) are mutable. Custom
classes are generally mutable. To simulate immutability in a class, one should
override attribute setting and deletion to raise exceptions.
Calculate the volume of an area, enclosed by two regions given by:
x2 + y 2 5 1, y 2 + z 2 5 1
10 island in a line. Two bridges between two consecutive island.
one is okay, one is broken. If you take the broken bridge, you will falls
into water and taken back to the start. start from the first island,find
the expected number of time u cross the non-broken bridges when
you reach the end.

23
Figure 14: integrals

We solve the problem on the understanding that we remember bad bridges.


It is not clear whether one is also expected to solve the amnesiac version.
For i = 1 to 9, let Xi = 1 if we take the wrong bridge in trying to get from
island i to island i +1. Let Xi = 0 otherwise.
The "extra" cost of a mistake in trying to get from i to i +1 is iXi . This
is because if Xi = 1 then we have spent one bridge crossing on the bad bridge,
and there are i − 1additional bridges we must cross.
P9
The total number of bridges is therefore 9 + i=1 iXi .
P9
By the linearity of expectation, the expectation of this is 9 + i=1 iE (Xi ).
Each Xi has expectation 12 . Thus our expectation is 9 + 12 (9)(10)
2 .
How to create an event with probability 1/16 from tossing biased
coin (51% head and 49% tail), exact method.
Suppose that you could create an event with probability 1/2 from such a coin,
creating an event with 1/16 is easy. But creating an event with a probability
1/2 is the same as creating a fair coin out of a biased coin, and is a well known

24
problem1 . This is what you do: Toss the coin twice. If both tosses turn out to
be the same, discard both tosses and repeat. When the two tosses give different
results, take the first one of them
It is easy to see that this procedure returns heads and tails with equal prob-
ability. Generating an event with probability 1/16 is now possible using this
method. Here is the pseudocode for simulating this, assuming that there is a
method cointoss() which returns ’H’ with probability 0.51 and ’T’ with proba-
bility 0.49
method getfairtoss()
var toss1,toss2
do
toss1=cointoss()
toss2=cointoss()
while(toss1==toss2)
return toss1
end method
method get4tosses()
return [getfairtoss() for i = 1 to 4]
end method
The probability that get4tosses() returns [’T’,’T’,’T’,’T’] is exactly 1/16 since
getfairtoss() returns ’T’ with probability 1/2.
Further explanation
Flip the biased coin twice; if the result is HH or TT, discard the case and
toss again until two consecutive toss is either HT or TH; then probability of
having HT or TH is equally 50/50; define this process so far as one set. Run 4
sets; if the result matches with (any) predefined order, accept it.
Consider a polynomial f(x) and its derivative f (x) that are related
according to: f(X) - f ‘(X) = X"3 + 3X"2 + 3X + 1 What is f(9)?
1 Fair results from a biased coin

If a cheat has altered a coin to prefer one side over another (a biased coin), the coin can still
be used for fair results by changing the game slightly. John von Neumann gave the following
procedure:
1. Toss the coin twice.
2. If the results match, start over, forgetting both results.
3. If the results differ, use the first result, forgetting the second.
The reason this process produces a fair result is that the probability of getting heads and then
tails must be the same as the probability of getting tails and then heads, as the coin is not
changing its bias between flips and the two flips are independent. This works only if getting
one result on a trial doesn’t change the bias on subsequent trials, which is the case for most
non-malleable coins (but not for processes such as the Polya urn). By excluding the events of
two heads and two tails by repeating the procedure, the coin flipper is left with the only two
remaining outcomes having equivalent probability. This procedure only works if the tosses are
paired properly; if part of a pair is reused in another pair, the fairness may be ruined. Also,
the coin must not be so biased that one side has a probability of zero.
This method may be extended by also considering sequences of four tosses. That is, if the
coin is flipped twice but the results match, and the coin is flipped twice again but the results
match now for the opposite side, then the first result can be used. This is because HHTT and
TTHH are equally likely. This can be extended to any power of 2.

25
f(9)=1366
We have a function F defined as: F(x)=11*x What is the value of
the ratio defined as the variance of F(x) divided by the variance of x?
V(F(x))/V(x)=V(11x)/V(x)=11^2*V(x)/V(x)=11^2=121
There are N(sufficiently large) numbers that may repeat and x
repeats more than N/2 times. Try to find an algorithm with time
O(N) and space O(1) to find x.
loop through the array,
setting a majority_element variable to be the first item and a counter = 1.
if the current_element is similar to the majority_element, increase the
counter by 1;
otherwise decrease the counter,
if the counter ever goes to zero, setting the majority_elemnent to cur-
rent_element. return majority_element.
FURTHER INFORMATION
Boyer–Moore majority vote algorithm
Initialize an element m and a counter i with i = 0
For each element x of the input sequence:
If i = 0, then assign m = x and i = 1
else
if m = x, then assign i = i + 1
else assign i = i − 1
Return m
A, B and C are time series and the following rhos represent their
correlation coefficients. rho(A,B)=0.7, rho(B,C)=0.8, what is rho(A,C)?
Why the correlation matrix is positive semi-definite?
0.13 <= rho(A,C) <= 0.99
regarding why the matrix must be positive definite: first, note that cov
matrix is also positive definite, so lets just discuss why that is the case. if
we diagonalize the covariance matrix, then the entries on the diagonal represent
variances in a basis that is just a linear combination of the original basis vectors.
These variances are the eigenvalues. Because variance must be >0, then we
have that our eigenvalues are > 0. this is the definition of a positive definite
matrix.
Since, it should follow positive semi-definite matrix rule. So, determinant of
the matrix should be positive -p^2 + 1.12 p - 0.13 > 0 0.13< p < 0.99
Given a time series, e.g. stock price, try to find an algorithm to
find the subsequence that maximizes {last element - first element} of
time O(n)

1. Traverse the input array from left to right.


2. For each distinct element map its first and last index in the hash table.
3. Traverse the hash table and calculate the first and last index difference
for each element.

26
Figure 15: one partition of stick

4. Accordingly update the max_diff.

Randomly cut a line segment into three pieces.What is the probability


that these three pieces can form a triangle?
Let the length of rope be 1 unit.
We choose two points X and Y on the rope.
Note: Formation of triangle is based on Triangle inequality i.e. sum of the
lengths of any two sides of a triangle must be greater than the length of the
third side
There are two possibilities of choosing points X and Y on rope:
Case 1: X < Y
Length of pieces after choosing points X and Y: X units, (Y-X) units, (1-Y)
units
Figure 14 shows the partition rope.
The 3 possible combination for satisfying Triangle Inequality
1. X + (Y-X) > (1-Y) => 2Y > 1 => Y > (1/2)
2. X + (1-Y) > (Y-X) => 2X + 1 > 2Y => Y < X + (1/2)
3. (Y-X) + (1-Y) > X => 2X < 1 => X < 1/2

Forming a graph using above 3 conditions and X<Y


The area in the region 1 of the graph is the required area which is: 1/2*1/2*1/2
= 1/8
Case 2: X > Y
Length of pieces after choosing points X and Y: Y units, (X-Y) units, (1-X)
units
Figure 16 shows the partition rope.
The 3 possible combinations for satisfying Triangle Inequality
1. Y + (X-Y) > (1-X) => 2X > 1 => X > (1/2)
2. Y + (1-X) > (X-Y) => 2Y + 1 > 2X => X < Y + (1/2)
3. (X-Y) + (1-X) > Y => 2Y < 1 => Y < 1/2
Forming a Graph using 3 condition and Y<X
The area in the region 5 of the graph is the required area which is: 1/2*1/2*1/2
= 1/8

27
Figure 16: when x<y

Figure 17: second option for braking a stick

28
Figure 18: When y<x

In both cases we get same required area, thus area required will be: 1/8. So
the Probability of breaking a rope into three pieces such that the sides form a
triangle is given by: Area required/Total area = (1/8)/(1/2*1*1) = 1/4 = 0.25.
You have a large jar containing 999 fair pennies and one two-
headed penny. Suppose you pick one coin out of the jar and flip it
10 times and get all heads, What is the probability that the coin you
chose is the two-headed one?
P(A) = 1/1000 (prior probability of choosing the two-headed penny) P(B)
= 999/1000 * (1/2)^10 + 1/1000 * 1 (the probability of having 10 heads in
a row is 1/2^10 if you chose any of the 999 fair coins, and 1 if you chose the
two-headed penny) P(B|A) = 1 (the probability of getting 10 heads given that
you have chosen the two-headed penny is 1)
By Bayes’ rule you have the probability that the coin is two-headed, given
the fact that there were 10 heads in a row, equal to:
P(A|B) = P(A) * P(B|A) / P(B) = 0.001 * 1 / 0.001975 = 50.62%
Consider a bubble sort algorithm dealing with 1~100. What is the
probability that after the first round of bubble, the 10th element is
in the 20th address?
1/420
A,B and C choose a integer between 1 and 100 in turn to mini-
mize the difference between the integer they choose and an unknown
uniformly distributed random integer. B knows what A chooses and
C knows what A,B choose. What is A’s optimal strategy?
If I choose any number between 2 and 99, B and C would choose the 2
numbers right next to it and I am 100% lose. However, if I choose 1 or 100, I
am ensure that if a number come up smaller than 1 (or bigger than 100) I am

29
gonna win -&gt; choose either 1 or 100.

30

You might also like