mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-11-29 07:16:09 +08:00
misc: prettier format all files
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
Databases
|
||||
==
|
||||
# Databases
|
||||
|
||||
## General
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
Networking
|
||||
==
|
||||
# Networking
|
||||
|
||||
- Given an IPv4 IP address p and an integer n, return a list of CIDR strings that most succinctly represents the range of IP addresses from p to (p + n).
|
||||
- Describe what happens when you enter a url in the web browser.
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
Security
|
||||
==
|
||||
# Security
|
||||
|
||||
## Encryption
|
||||
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
Snake Game
|
||||
==
|
||||
# Snake Game
|
||||
|
||||
Design a snake game that is to be played in web browser.
|
||||
|
||||
Client: React + Redux
|
||||
|
||||
Rendering:
|
||||
Pixel-based graphics. Depending on the intended resolution, can divide the screen into N * M pixels. Can dynamically calculate the size of each pixel.
|
||||
Pixel-based graphics. Depending on the intended resolution, can divide the screen into N \* M pixels. Can dynamically calculate the size of each pixel.
|
||||
|
||||
Fruit: One pixel.
|
||||
Snake body: One pixel width made up of connected pixels.
|
||||
|
||||
Model:
|
||||
|
||||
```js
|
||||
{
|
||||
fruit: {
|
||||
x, y
|
||||
@@ -23,7 +24,9 @@ Model:
|
||||
speed: 500,
|
||||
points: 0
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
function update() {
|
||||
next_loc = points[0] + (x, y) # Depends on the direction
|
||||
if (snake.points.find(next_loc) > 0) {
|
||||
@@ -33,39 +36,41 @@ function update() {
|
||||
if (!isEqual(next_loc, fruit)) {
|
||||
pts = points.removeLast();
|
||||
} else {
|
||||
generate_fruit();
|
||||
generateFruit();
|
||||
points++;
|
||||
}
|
||||
snake.points = [next_loc, ...pts];
|
||||
|
||||
// Boundary checking -> die
|
||||
}
|
||||
```
|
||||
|
||||
function generate_fruit() {
|
||||
```js
|
||||
function generateFruit() {
|
||||
// Cannot generate on my own body.
|
||||
|
||||
// First approach: while on body, generate
|
||||
let next_fruit_location = random_location();
|
||||
while (snake.points.find(next_fruit_location) > 0) {
|
||||
next_fruit_location = random_location();
|
||||
next_fruit_location = random_location();
|
||||
}
|
||||
fruit = next_fruit_location
|
||||
fruit = next_fruit_location;
|
||||
|
||||
// Second approach: brute force
|
||||
for (let i = 0; i < rows; i++) {
|
||||
for (let j = 0; j < cols; j++) {
|
||||
let point = { x: i, y: j }
|
||||
let point = { x: i, y: j };
|
||||
if (snake.points.find(next_fruit_location) === -1) {
|
||||
fruit = point
|
||||
fruit = point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Third approach: brute force with random
|
||||
const available_points = []
|
||||
const available_points = [];
|
||||
for (let i = 0; i < rows; i++) {
|
||||
for (let j = 0; j < cols; j++) {
|
||||
let point = { x: i, y: j }
|
||||
let point = { x: i, y: j };
|
||||
if (snake.points.find(next_fruit_location) === -1) {
|
||||
available_points.push(point);
|
||||
}
|
||||
@@ -75,3 +80,4 @@ function generate_fruit() {
|
||||
}
|
||||
|
||||
setInterval(update, speed);
|
||||
```
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
Software Engineering
|
||||
==
|
||||
# Software Engineering
|
||||
|
||||
## What is the difference between an interface and abstract class?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user