The first question

Taxi interview questions are as follows:

  • When taking a taxi, you can take a special bus or an express train, and any car has its license plate number and name.
  • The price varies from car to car, express 1 yuan per kilometer, special 2 yuan per kilometer.
  • Display vehicle information at the start of the trip.
  • At the end of the trip, show the amount of the taxi (assuming the trip is only 5 km)
// Abstract car class
class Car {
  constructor(name, number) {
    this.name = name;
    this.number = number; }}/ / class
class Trip {
  constructor(car) {
    this.car = car;
  }
  start() {
    console.log('Start of journey, vehicle:The ${this.car.name}, license plate number:The ${this.car.number}. `);
  }
  end () {
    console.log('End of trip, vehicle:The ${this.car.name}, taxi amount:The ${this.car.money * 5}}. `); }}// Express train
class FastCar extends Car{
  constructor(name, number) { 
    super(name, number);
    this.money = 2; }}// Specific type of car
class SpecialCar extends Car{
  constructor(name, number) {
    super(name, number);
    this.money = 1; }}/ / instantiate
const newCar = new SpecialCar('car'.'110');
const newTrip = new Trip(newCar);
newTrip.start();
newTrip.end();
Copy the code

The second question

Taxi interview questions are as follows:

  • A parking lot, divided into 3 floors, each floor of 100 parking Spaces
  • Every parking spot can monitor vehicles entering and leaving
  • Display the number of free parking Spaces on each floor before vehicles enter
  • When a car enters, the camera recognizes the license plate number and time
  • When the vehicle exits, the exit display shows the license plate number and the parking duration
/ / car
class Car {
  constructor(number) {
    this.number = number; }}/ / camera
class Camera {
  shot(car) {
    return {
      number: car.number,
      inTime: Date.now()
    }
  }
}
class Screen {
  show(car, inTime) {
    console.log('License plate number:${car.number}`);
    console.log('Parking duration:The ${(Date.now() - inTime) / 1000}Second `); }}/ / parking
class Place {
  constructor() {
    this.empty = true;
  }
  in() {
    this.empty = false;
  }
  out() {
    this.empty = true; }}/ / layer
class Floor {
  constructor(index, places) {
    this.index = index;
    this.places = places || [];
  }
  getEmptyPlaces() {
    let count = 0;
    this.places.forEach(place= > {
      if (place.empty) count++;
    });
    returncount; }}/ / parking
class Park {
  constructor(floors) {
    / / the camera
    this.camera = new Camera();
    / / display
    this.screen = new Screen();
    // All parking Spaces
    this.floors = floors || [];
    // Number of parking floors
    this.floorsLen = floors.length || 0;
    // Use it to store parked cars
    this.carMap = {};
  }
  / / in the top
  beforeIn() {
    let allFloorCount = 0;
    let res = ' ';
    this.floors.forEach(floor= > {
      const currentCount = floor.getEmptyPlaces();
      res += 'Remaining parking Spaces on the current floor:${currentCount}A \ n `;
      allFloorCount += currentCount;
    });
    return res += 'Total remaining parking lots:${allFloorCount}A `;
  }
  / / to enter
  in(car) {
    let info = this.camera.shot(car);
    // // Random layer
    // const randomFloor = parseInt(Math.random() * this.floorsLen) + 1;
    // Random parking position
    const randomPlace = parseInt(Math.random() * 100);
    // The problem of overloading is not further discussed
    const place = this.floors[0].places[randomPlace];
    // Enter the parking space
    place.in();
    info.place = place;
    / / store
    this.carMap[info.number] = info;
  }
  / / out
  out(car) {
    const info = this.carMap[car.number];
    // Get out of the parking space
    info.place.out();
    // Display information on screen
    this.screen.show(info, info.inTime);
    // Delete the storage
    Reflect.deleteProperty(this.carMap, car.number); }}// Initialize the parking lot
const floors = [];
for (let i = 0; i < 3; i++) {
  const places = [];
  for (let j = 0; j < 100; j++) {
    places[j] = new Place();
  }
  floors[i] = new Floor(i + 1, places);
}
const park = new Park(floors);

// Initialize the vehicle
const car1 = new Car(100);
const car2 = new Car(200);
const car3 = new Car(300);

console.log('First vehicle entering');
console.log(park.beforeIn());
park.in(car1);
console.log('Second vehicle entering');
console.log(park.beforeIn());
park.in(car2);
console.log('First car leaving');
park.out(car1);
console.log('Second vehicle leaving');
park.out(car2);

console.log('Third vehicle entering');
console.log(park.beforeIn());
park.in(car3);
console.log('Third vehicle leaving.');
park.out(car3);
Copy the code