Skip to content

Commit bb31b65

Browse files
committed
Updates triangular-series.md
Auto commit by GitBook Editor
1 parent 94df7f9 commit bb31b65

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

SUMMARY.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Summary
2+
3+
* [Introduction](README.md)
4+
* Interview Cake
5+
* [Two Egg Problem](two-egg-problem.md)
6+
* [Two Egg Problem - Solution](two-egg-problem-solution.md)
7+
* [Triangular Series](triangular-series.md)
8+

triangular-series.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Triangular Series
2+
3+
A **triangular series **is a series of numbers where each number could be the row of an equilateral triangle.
4+
5+
So 1, 2, 3, 4, 5 is a triangular series, because you could stack the numbers like this:
6+
7+
![](https://www.interviewcake.com/images/svgs/triangular_series__triangle_of_stacked_circles.svg?bust=202 "Rows of 1, 2, 3, 4, and 5 circles to show how numbers in a triangular series can be stacked to form a triangle.")
8+
9+
Their sum is 15, which makes 15 a **triangular number**.
10+
11+
A triangular series _always _starts with 1 and increases by 1 with each number.
12+
13+
Since the only thing that changes in triangular series is the value of the highest number, it’s helpful to give that a name. Let’s call it **n**.
14+
15+
```
16+
# n is 8
17+
1, 2, 3, 4, 5, 6, 7, 8
18+
```
19+
20+
Triangular series are nice because no matter how largennis, it’s always easy to find the total sum of all the numbers.
21+
22+
Take the example above. Notice that if we add the first and last numbers together, and then add the second and second-to-last numbers together, they have the same sum! This happens with _every _pair of numbers until we reach the middle. If we add up all the pairs of numbers, we get:
23+
24+
```
25+
1 + 8 = 9
26+
2 + 7 = 9
27+
3 + 6 = 9
28+
4 + 5 = 9
29+
```
30+
31+
This is true for_every triangular series_:
32+
33+
1. **Pairs of numbers **on each side will always **add up to the same value**.
34+
2. That value will always be **1 more than the series’ n**.
35+
36+
This gives us a pattern. Each pair's sum is `n+1`, and there are `n/2` ​pairs. So our total sum is:
37+
38+
$$(n+1) ∗ n/2
39+
$$
40+
41+
Or:
42+
43+
$$(n2 + n)/2$$
44+
45+
Ok, but does this work with triangular series with an _odd _number of elements? Yes. Let's say `n = 5`. So if we calculate the sum by hand:
46+
47+
```
48+
1+2+3+4+5 = 15
49+
```
50+
51+
And if we use the formula, we get the same answer:
52+
53+
$$(5 * 5 + 5)/2 = 15$$
54+
55+
One more thing:
56+
57+
**What if we know **_**the total sum**_**, but we **_**don't **_**know the value of n?**
58+
59+
Let’s say we have:
60+
61+
```
62+
1 + 2 + 3 + … + (n−2) + (n−1) + n = 78
63+
```
64+
65+
No problem. We just use our formula and set it equal to the sum!
66+
67+
$$(n2 + n)/2 = 78$$
68+
69+
Now, we can rearrange our equation to get a _quadratic equation _\(remember those?\)
70+
71+
$$n2 + n = 156$$
72+
73+
$$n2 + n − 156 = 0$$
74+
75+
Here's the quadratic formula:
76+
77+
$$(−b ± Sqrt(b2 − 4ac)/2a)$$
78+
79+
If you don't really remember how to use it, that's cool. You can just use an online calculator. We don't judge.
80+
81+
Taking the positive solution, we get `n = 12`.
82+
83+
So for a triangular series, remember—the total sum is:
84+
85+
$$(n2 + n)/2$$
86+
87+
88+
89+

two-egg-problem-solution.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Two Egg Problem - Solution
2+
3+
**We'll use **_**the first egg **_**to get a**_**range of possible floors **_that contain the highest floor an egg can be dropped from without breaking. To do this, we'll drop it from increasingly higher floors until it breaks, skipping some number of floors each time.
4+
5+
When the first egg breaks,**we'll use **_**the second egg**_**to find the **_**exact **_**highest floor **an egg can be dropped from. We only have to drop the second egg starting from the last floor where the first egg _didn't _break, up to the floor where the first egg _did _break. But we have to drop the second egg one floor at a time.
6+
7+
With the first egg, if we skip the _same number of floors every time_, the worst case number of drops will increase by one _every _time the first egg doesn't break. To counter this and keep our worst case drops _constant_, **we decrease the number of floors we skip **_**by one **_**each time we drop the first egg**.
8+
9+
When we're choosing how many floors to skip the _first _time we drop the first egg, we know:
10+
11+
1. **We want to skip as few floors as possible,**
12+
so if the first egg breaks right away we don't have a lot of floors to drop our second egg from.
13+
2. **We **_**always **_**want to be able to reduce the number of floors we're skipping.**
14+
We don't want to get towards the top and not be able to skip floors any more.
15+
16+
Knowing this, we set up the following equation wherennis the number of floors we skip the first time:
17+
18+
$$n+(n−1)+(n−2)+…+1=100$$
19+
20+
This triangular series reduces to `n * (n+1) / 2 = 100` which solves to give `n = 13.651`. We round up to 14 to be safe. So our first drop will be from the 14th floor, our second will be 13 floors higher on the 27th floor and so on until the first egg breaks. Once it breaks, we'll use the second egg to try every floor starting with the last floor where the first egg didn't break. At worst, we'll drop both eggs a combined total of 14 times.
21+
22+
For example:
23+
24+
```
25+
Highest floor an egg won't break from
26+
13
27+
28+
Floors we drop first egg from
29+
14
30+
31+
Floors we drop second egg from
32+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
33+
34+
Total number of drops
35+
14
36+
```
37+
38+
```
39+
Highest floor an egg won't break from
40+
98
41+
42+
Floors we drop first egg from
43+
14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99
44+
45+
Floors we drop second egg from
46+
96, 97, 98
47+
48+
Total number of drops
49+
14
50+
```
51+
52+
### What We Learned
53+
54+
This is one of our most contentious questions. Some people say, "Ugh, this is useless as an interview question," while others say, "We ask this at my company, it works great."
55+
56+
The bottom line is some companies _do _ask questions like this, so it's worth being prepared. There are a bunch of these not-exactly-programming interview questions that lean on math and logic. There are some famous ones about [shuffling cards](https://www.interviewcake.com/question/shuffle) and [rolling](https://www.interviewcake.com/question/simulate-5-sided-die)[dice](https://www.interviewcake.com/question/simulate-7-sided-die). If math isn't your strong suit, don't fret. It only takes a few practice problems to get the hang of these.
57+

two-egg-problem.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Two Egg Problem
2+
3+
**A building has 100 floors. One of the floors is the highest floor an egg can be dropped from without breaking.**
4+
5+
If an egg is dropped from above that floor, it will break. If it is dropped from that floor or below, it will be completely undamaged and you can drop the egg again.
6+
7+
Given two eggs, find the highest floor an egg can be dropped from without breaking, with as few drops as possible.
8+
9+
### Breakdown
10+
11+
What if we only had _one _egg? How could we find the correct floor?
12+
13+
Because we can't use the egg again if it breaks, we'd have to play it safe and drop the egg from _every _floor, starting at the bottom and working our way up. In the worst case, the egg won't break until the top floor, so we'd drop the egg 100 times.
14+
15+
What does having _two _eggs allow us to do differently?
16+
17+
Since we have two eggs, we can skip multiple floors at a time until the first egg breaks, keeping track of which floors we dropped it from. Once that egg breaks we can use the second egg to try every floor, starting on the last floor where the egg _didn't _break and ending on the floor below the one where it_did_break.
18+
19+
**How should we choose how many floors to skip with the first egg?**
20+
21+
What about trying a binary approach? We could drop the first egg halfway up the building at the 50th floor. If the egg doesn't break, we can try the 75th floor next. We keep going like this, dividing the problem in half at each step. As soon as the first egg breaks, we can start using our second egg on our \(now-hopefully narrow\) range of possible floors.
22+
23+
If we do that, what's the **worst case number of total drops?**
24+
25+
The worst case is that the highest floor an egg won't break from is floor 48 or 49. We'd drop the first egg from the 50th floor, and then we'd have to drop the second egg _from every floor _from 1 to 49, for a total of 50 drops. \(Even if the highest floor an egg won't break from is floor 48, we still won't know if it will break from floor 49 until we try.\)
26+
27+
**Can we do better than this binary approach?**
28+
29+
50 is probably too many floors to skip for the first drop. In the worst case, if the first egg breaks after a _small _number of drops, the second egg will break after a _large _number of drops. And if we went the _other _way and skipped **1 **floor every time, we'd have the _opposite _problem! What would the worst case floor be then?
30+
31+
The worst case would be floor 98 or 99—the first egg would drop a _large _number of times \(at every floor from 2-100 skipping one floor each time\) and the last egg would drop a _small _number of times \(only on floor 99\), for a total of 51 drops.
32+
33+
Can we balance this out? Is there some number between 50 and 1—the number of floors we'll skip with each drop of the first egg—where the first and second eggs would drop close to the _same _number of times in the worst case?
34+
35+
Yes, we could skip 10 floors each time. The worst case would again be floor 98 or 99, but we'd only drop the first egg 10 times and the second egg 9 times, for a total of **19 drops!**
36+
37+
**Is that the best we can do?**
38+
39+
Let's look at what happens with this strategy_each time the first egg doesn't break_.**How does the worst case total number of drops change?**
40+
41+
The worst case total number of drops _increases by one each time the first egg doesn't break_
42+
43+
For example, if the egg breaks on our first drop from the 10th floor, we may have to drop the second egg at each floor between 1 and 9 for a worst case of 10 total drops. But if the egg breaks when we skip to the _20th _floor we will have a worst case of _11 _total drops \(once for the 10th floor, once for the 20th, and all of the floors between 11 and 19\)!
44+
45+
**How can we keep the worst case number of drops from increasing each time the first egg doesn't break?**
46+
47+
Since the maximum number of drops increases by one each time we skip the _same amount of floors_, we could skip _one fewer floor _each time we drop the first egg!
48+
49+
But how do we choose how many floors to skip the _first _time?
50+
51+
Well, we know two things that can help us:
52+
53+
1. **We want to skip as few floors as possible the **_**first **_**time we drop an egg,**
54+
so if our first egg breaks right away we don't have a lot of floors to drop our second egg from.
55+
2. **We **_**always **_**want to be able to reduce the number of floors we're skipping **_**by one**_**.**
56+
We don't want to get towards the top and not be able to skip any floors any more.
57+
58+
Now that we know this, can we figure out the ideal number of floors to skip the first time?
59+
60+
To be able to decrease the number of floors we skip by one _every time _we move up, and to minimize the number of floors we skip the first time, we want to end up skipping _just one floor at the very top_. Can we model this with an equation?
61+
62+
Let's say **n** is the number of floors we'll skip the first time, and we know 1 is the number of floors we'll skip last. Our equation will be:
63+
64+
```
65+
n+(n−1)+(n−2)+…+1=100
66+
```
67+
68+
How can we solve for **n**? Notice that we're summing every number from 1 to n.
69+
70+
The left side is atriangular series. Knowing this, can we simplify and solve the equation?
71+
72+
We know the left side reduces to:
73+
74+
$$(n2 + n) / 2$$
75+
76+
so we can plug that in:
77+
78+
$$(n2 + n) / 2 = 100$$
79+
80+
and we can rearrange to get a quadratic equation:
81+
82+
$$(n2 + n) / 2 - 200 = 0$$
83+
84+
which gives us **13.651**
85+
86+
We can't skip a fraction of a floor, so how many floors should we skip the first time? And what's our final **worst case total number of drops**?
87+

0 commit comments

Comments
 (0)