ch6 and some ch7 exercises
This commit is contained in:
parent
f4f0f3b332
commit
797fbae601
|
|
@ -0,0 +1,40 @@
|
||||||
|
# 6.7. The Apocalypse
|
||||||
|
|
||||||
|
> In the new post-apocalyptic world, the world queen is desperately concerned about the birth rate. Therefore, she decrees that all families should ensure that they have one girl or else they face massive fines. If all families abide by this policy-that is, they have continue to have children until they have one girl, at which point they immediately stop-what will the gender ratio of the new generation be? (Assume that the odds of someone having a boy or a girl on any given pregnancy is equal.) Solve this out logically and then write a computer simulation of it.
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
|
||||||
|
> Observe that each family will have exactly one girl.
|
||||||
|
|
||||||
|
So it can either be 1 girl - 0 boy, 1-1, 1-2, 1-3. So 1-1/(0.5)^n.
|
||||||
|
|
||||||
|
> Write each family as a sequence of Bs and Gs
|
||||||
|
|
||||||
|
* G
|
||||||
|
* BG
|
||||||
|
* BBG
|
||||||
|
* BBBG
|
||||||
|
* ...
|
||||||
|
* it becomes more and more improbable
|
||||||
|
|
||||||
|
|
||||||
|
> You can attempt this mathematically, although the math is pretty difficult. You might find it easier to estimate it up to families of, say, 6 children. This won't give you a good mathematical proof, but it might point you in the right direction of what the answer might be.
|
||||||
|
|
||||||
|
With 6 children, ratios might be
|
||||||
|
|
||||||
|
* 1-0, with prob 1/2
|
||||||
|
* 1-1, with prob 1/2 * 1/2
|
||||||
|
* 1-2, with prob (1/2)^3
|
||||||
|
* 1-3, with prob (1/2)^4
|
||||||
|
* 1-4, with prob (1/2)^5
|
||||||
|
* 1-5, with prob (1/2)^6
|
||||||
|
|
||||||
|
> Logic might be easier than math. Imagine we wrote every birth into a giant string of Bs and Gs. Note that the groupings of families are irrelevant for this problem. What is the probability of the next character added to the string being a B versus a G?
|
||||||
|
|
||||||
|
1/2
|
||||||
|
|
||||||
|
> Observe that biology hasn't changed; only the conditions under which a family stops having kids has changed. Each pregnancy has a 50% odds of being a boy and a 50% odds of being a girl.
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
On average, families will have 1 boy and 1 girl. So the ratio is 50%.
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# 6.8. The egg drop problem
|
||||||
|
|
||||||
|
> There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it's dropped from any floor below, it will not break.You're given two eggs. Find N, while minimizing the number of drops for the worst case.
|
||||||
|
|
||||||
|
Hints:#156, #233, #294, #333, #357, #374, #395
|
||||||
|
|
||||||
|
I'm going to need log(100) eggs to find N.
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
|
||||||
|
> As a first approach, you might try something like binary search. Drop it from the 50th floor, then the 75th, then the 88th, and so on. The problem is that if the first egg drops at the 50th floor, then you'll need to start dropping the second egg starting from the 1st floor and going up.This could take, at worst, 50 drops (the 50th floor drop, the 1st floor drop, the 2nd floor drop, and up through the 49th floor drop). Can you beat this?
|
||||||
|
|
||||||
|
I don't understand the worst case scenario.
|
||||||
|
|
||||||
|
> It's actually better for the first drop to be a bit lower. For example, you could drop at the 10th floor, then the 20th floor, then the 30th floor, and so on.The worst case here will be 19 drops (10, 20, ..., 100, 91, 92, ... , 99). Can you beat that? Try not randomly guessing at different solutions. Rather, think deeper. How is the worst case defined? How does the number of drops of each egg factor into that?
|
||||||
|
|
||||||
|
Still don't get it.
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# Hash table
|
# 7.12. Hash table
|
||||||
|
|
||||||
## Design and implement a hash table which uses chaining (linked lists) to handle collissions
|
> Design and implement a hash table which uses chaining (linked lists) to handle collissions
|
||||||
|
|
||||||
## Non-OOP solution
|
## Non-OOP solution
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
# 7.2. Call center
|
||||||
|
|
||||||
|
> Imagine you have a call center with three levels of employees: respondent, manager, and director. An incoming telephone call must be first allocated to a respondent who is free. If the respondent can't handle the call, he or she must escalate the call to a manager. If the manager is not free or not able to handle it, then the call should be escalated to a director. Design the classes and data structures for this problem. Implement a method dispatchCall() which assigns a call to the first available employee.
|
||||||
|
|
||||||
|
Hints 363
|
||||||
|
|
||||||
|
There are 3 types of employees, and a number of them. 5 respondents why not, 1 manager and 1 director. In dispatch call, we check the respondents. If there's one free, they take it and they become busy. If all are busy, the manager is called. If the manager is busy, the director is called.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class CallCenter {
|
||||||
|
private static int respondent;
|
||||||
|
private static int manager;
|
||||||
|
private static int director;
|
||||||
|
protected CallCenter() {
|
||||||
|
respondent = 5;
|
||||||
|
manager = 1;
|
||||||
|
director = 1;
|
||||||
|
}
|
||||||
|
protected dispatchCall() {
|
||||||
|
if (respondent > 0) {
|
||||||
|
respondent--;
|
||||||
|
} else if (manager > 0) {
|
||||||
|
manager--:
|
||||||
|
}
|
||||||
|
else if (director > 0) {
|
||||||
|
director--;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
Make a class for each employee type. All employees have common traits like address, name, job title, age. These can be kept all in one class. There shold be a CallHandler class that routes the calls to the correct person.
|
||||||
|
|
||||||
|
[Solution codes](https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2007.%20Object-Oriented%20Design/Q7_02_Call_Center)
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
# 7.4. Parking lot
|
||||||
|
|
||||||
|
> Design a parking lot using object-oriented principles
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class ParkingLot {
|
||||||
|
private final int NUMSPOTS = 1000;
|
||||||
|
private final int NORMALHFEE = 0.25;
|
||||||
|
private final Time OPENINGH = new Time(800);
|
||||||
|
private final Time CLOSINGH = new Time(2000);
|
||||||
|
protected ArrayList<Car> allCars = new ArrayList<Car>();
|
||||||
|
private int carsInParking = 0;
|
||||||
|
|
||||||
|
public int hiCar(Time comingtime) {
|
||||||
|
if (carsInParking < NUMSPOTS) {
|
||||||
|
if (comingtime < OPENINGH || comingtime > CLOSINGH) {
|
||||||
|
allCars.push(new Car(comingtime));
|
||||||
|
carsInParking++;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
The car can only be retrieved during 8:00 and 20:00, but many days later.
|
||||||
|
*/
|
||||||
|
private int calculateCost(Car myCar) {
|
||||||
|
return (myCar.DEPARTUREH - myCar.ARRIVALH) * NORMALHFEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int byeCar(Car myCar, Time goingtime) {
|
||||||
|
if (carsInParking < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!allCars.contains(myCar)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (goingtime < myCar.ARRIVALH) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
myCar.setDepTime(goingtime);
|
||||||
|
allCars.pop(myCar);
|
||||||
|
carsInParking--;
|
||||||
|
Car.setCost(calculateCost(myCar));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
private Time ARRIVALH = null;
|
||||||
|
private Time DEPARTUREH = null;
|
||||||
|
private int cost;
|
||||||
|
|
||||||
|
protected Car(Time arrivaltime) {
|
||||||
|
ARRIVALH = arrivaltime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepTime(Time goingtime) {
|
||||||
|
DEPARTUREH = goingtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCost(int parkingcost) {
|
||||||
|
cost = parkingcost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
Think about types of vehicles, and spots for these vehicles.
|
||||||
|
|
@ -1,5 +1,48 @@
|
||||||
# Chapter 7 Object oriented design
|
# Chapter 7 Object oriented design
|
||||||
|
|
||||||
|
OOP questions are about demonstrating that you understand how to create elegant, maintainable object-oriented code.
|
||||||
|
|
||||||
|
1. Handle ambiguity: many questions are intentionally vague, so that you ask questions. Who's going to use it, how.
|
||||||
|
2. Define the core objects: if it's a restaurant, the core objects might be table, guest, party, etc.
|
||||||
|
3. Analyze relationships between the objects. Which objects are member of other objects, which inherit from which, are relationships many-to-many or one-to-many? Party should have an array of guests, server and host inherit from employee, etc.
|
||||||
|
4. Investigate actions: what will objects do
|
||||||
|
|
||||||
|
## Design patterns
|
||||||
|
|
||||||
|
### Singleton class
|
||||||
|
|
||||||
|
This pattern ensures that a class has only one instance and ensures access to the instant through the application, it can be useful where you have a global object with exactly one instance. We might want to implement `Restaurant` with exactly one instance of Restaurant. Many people dislike this pattern, because it can interfere with unit testing.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Restaurant {
|
||||||
|
private static Restaurant _instance = null;
|
||||||
|
protected Restaurant() {...}
|
||||||
|
public static Restaurant getInstance() {
|
||||||
|
if (_instance == null) {
|
||||||
|
_instance = new Restaurant();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Factory method
|
||||||
|
|
||||||
|
It offers an interface for creating an instance of a class, with its subclasses deciding which class to instantiate.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class CardGame {
|
||||||
|
public static CardGame createCardGame (GameType type) {
|
||||||
|
if (type == Gametype.Poker) {
|
||||||
|
return new PokerGame();
|
||||||
|
} else if (type == Gametype.BlackJack) {
|
||||||
|
return new BlackJackGame();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Resouces
|
## Resouces
|
||||||
|
|
||||||
* [C++ solutions](https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2007.%20Object-Oriented%20Design)
|
* [C++ solutions](https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2007.%20Object-Oriented%20Design)
|
||||||
Loading…
Reference in New Issue