|
178 | 178 | <a name="design-db"></a> |
179 | 179 | ### Database Design |
180 | 180 |
|
| 181 | +#### Concepts |
| 182 | + |
181 | 183 | * ACID |
182 | 184 | - **Atomicity**: Transactions are often composed of multiple statements. Atomicity guarantees that each transaction is treated as a single "unit", which either succeeds completely, or fails completely: if any of the statements constituting a transaction fails to complete, the entire transaction fails and the database is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes. |
183 | 185 |
|
|
187 | 189 |
|
188 | 190 | - **Durability**: guarantees that once a transaction has been committed, it will remain committed even in the case of a system failure (e.g., power outage or crash). This usually means that completed transactions (or their effects) are recorded in non-volatile memory. |
189 | 191 |
|
| 192 | +#### SQL Query |
| 193 | + |
| 194 | + * For DB tables: |
| 195 | + |
| 196 | + ```sql |
| 197 | + -- containers(id, name); |
| 198 | + CREATE TABLE IF NOT EXISTS containers ( |
| 199 | + `id` INT NOT NULL AUTO_INCREMENT COMMENT 'PK for containers', |
| 200 | + `name` VARCHAR(45) NULL COMMENT 'container name', |
| 201 | + PRIMARY KEY (`id`) |
| 202 | + ); |
| 203 | + |
| 204 | + -- items(id, type); |
| 205 | + CREATE TABLE IF NOT EXISTS items ( |
| 206 | + `id` INT NOT NULL AUTO_INCREMENT COMMENT 'PK for items', |
| 207 | + `type` VARCHAR(45) NULL COMMENT 'Item type', |
| 208 | + PRIMARY KEY (`id`) |
| 209 | + ); |
| 210 | + |
| 211 | + -- containeritem(container_id, item_id); |
| 212 | + CREATE TABLE IF NOT EXISTS containeritem ( |
| 213 | + `container_id` INT NOT NULL COMMENT 'Composite PK: FK to containers.id', |
| 214 | + `item_id` INT NOT NULL COMMENT 'Composite PK: FK to items.id', |
| 215 | + CONSTRAINT `FK__containeritem_container_id` |
| 216 | + FOREIGN KEY (`container_id`) |
| 217 | + REFERENCES `containers` (`id`), |
| 218 | + CONSTRAINT `FK__containeritem_item_id` |
| 219 | + FOREIGN KEY (`item_id`) |
| 220 | + REFERENCES `items` (`id`) |
| 221 | + ); |
| 222 | + ``` |
| 223 | + Write a SQL query to list all container names, item types, and count of types (including zero) for each container. |
| 224 | + |
| 225 | + ```sql |
| 226 | + SELECT d.name, d.type, |
| 227 | + CASE WHEN ci.item_id IS NULL THEN 0 ELSE count(ci.item_id) END as 'count' |
| 228 | + FROM ( |
| 229 | + SELECT c.id as 'c_id', c.name, i.id as 'i_id', i.type |
| 230 | + FROM containers c |
| 231 | + CROSS JOIN items i |
| 232 | + ) d -- optionally to use CTE |
| 233 | + LEFT OUTER JOIN containeritem ci ON d.i_id = ci.item_id |
| 234 | + AND d.c_id = ci.container_id |
| 235 | + GROUP BY d.name, d.type |
| 236 | + ORDER BY d.name, d.type |
| 237 | + ; |
| 238 | + ``` |
| 239 | + See http://sqlfiddle.com/#!9/20fa2b/52 |
| 240 | + |
190 | 241 |
|
191 | 242 | <a name="design-system"></a> |
192 | 243 | ### System Design |
193 | 244 |
|
| 245 | +#### Checklist |
| 246 | + |
194 | 247 | * A.B.C.D. |
195 | 248 | - **_A_**sk questions (what features? how much to scale? ...) |
196 | 249 | - Don't use **_b_**uzzword |
|
287 | 340 | * HAProxy |
288 | 341 |
|
289 | 342 |
|
| 343 | +#### Example: Find the peakest time |
| 344 | + |
| 345 | + From a long list of trip time (with Start and End time, sorted by Started), e.g. within a whole month, find the peakest trip time.¶ |
| 346 | + |
| 347 | + ```go |
| 348 | + type Trip struct { |
| 349 | + Start time |
| 350 | + End time |
| 351 | + } |
| 352 | + |
| 353 | + // assuming the list (e.g. a whole month/year) is sorted by start time |
| 354 | + func countPeakTrips(a []Trip) int { |
| 355 | + |
| 356 | + } |
| 357 | + ``` |
| 358 | + |
| 359 | + |
| 360 | +#### Example: Design a stock data system |
| 361 | + |
| 362 | + * requirements |
| 363 | + - get real time price |
| 364 | + - get historic trend |
| 365 | + * data schema and database |
| 366 | + * misc consideration |
| 367 | + |
290 | 368 |
|
291 | 369 | ### Resources: |
292 | 370 |
|
|
575 | 653 |
|
576 | 654 |
|
577 | 655 | <br/><a name="non-tech"></a> |
578 | | -### Non-technical |
| 656 | +## Non-technical |
579 | 657 |
|
580 | 658 | * [Psychological Tricks](https://github.com/yangshun/tech-interview-handbook/blob/master/non-technical/psychological-tricks.md) |
581 | 659 | * [Questions to ask](https://github.com/yangshun/tech-interview-handbook/blob/master/non-technical/questions-to-ask.md) |
|
584 | 662 |
|
585 | 663 |
|
586 | 664 | <br/><a name="reading"></a> |
587 | | -### Reading |
| 665 | +## Reading |
588 | 666 |
|
589 | 667 | * Books/Collection: |
590 | 668 | * [Computer Science in JavaScript](https://github.com/humanwhocodes/computer-science-in-javascript) |
|
0 commit comments