Solution | Front-end interview question analysis of the US Mission

Analysis of Meituan Interview Questions

Short answer

1. Question: Are the following variables i, s, a on the heap or on the stack? What is the value of the last executed ai?
class A {
    String i = "op";
    void func(String s) {
        s = ""+9;
    }
    static void test() {
        A a = new A();
       a.func(a.i);
    }
} 
  • Investigation points
    Stack memory
    Stack memory is mainly used to store various basic types of variables, including Boolean, Number, String, Undefined, Null and pointers to object variables

    Heap memory

    Heap memory mainly stores object objects

    Note: A is a new object, stored in the heap memory, and the corresponding address is the content of the pointer a, so a is still in the stack memory

    Parameter passing (study passing by value)

    The parameters of all functions in ECMAScript are passed by value

  • Problem analysis
    i, s, a are still basic types of variables, so they are still stored in stack memory

    a是A的一个实例,所以a.i='op',执行a.func(a.i)函数,相当于传入'op'参数,该函数会赋值一个变量,两个变量是完全独立的,s=''+9只是在复制的变量上执行的,函数体外的a.i并没有改变
    

2. Please write down the print results in order and explain the reasons.
var name = 'global';
var obj = {
    name: 'local',
    foo: function(){
        this.name = 'foo';
    }.bind(window)
};
var bar = new obj.foo();
setTimeout(function() {
    console.log(window.name);
}, 0);
console.log(bar.name);
 
var bar3 = bar2 = bar;
bar2.name = 'foo2';
console.log(bar3.name);
  • Inspection point
    Event Loop

    Synchronous and asynchronous different situations, and the use of timers

    Bind

    The problem of different binding priorities

    Reference type assignment

    The assignment of complex type values ​​is assignment by reference, which all point to the same object

  • Problem analysis'foo
    ' //bind returns a function, this function is bound to the window, the function is constructed by new, and a new object is returned. This in the function body points to the object, and bind is Hard binding, new binding has a higher priority than hard binding, so this is still bound to ban, bar.name=this.name='
    foo''foo2' //var bar3=bar2=bar; complex type The value assignment refers to the assignment, bar3, bar2, and bar all point to the same object, so when bar2 is modified, the contents of bar3 and bar will also be modified, so bar3.name=bar2.name='foo2'
    'global' //setTimeout sets a timer, which will place the execution of the callback at the end of all events and cannot jump in the queue, so the output of console.log(window.name) is the name='global' under the global


3. Question: Please write down the results produced after the following code is run, and give an explanation of how the results are obtained.
setTimeout(() => console.log('a'));
Promise.resolve().then(
   () => console.log('b’);
 ).then(
   () => Promise.resolve('c').then(
     (data) => {
       setTimeout(() => console.log('d'));
       console.log('f');
       return data;
     }
   )
 ).then(data => console.log(data));
  • Inspection point

    The order of execution of promise (macro task) and setTimeout (micro task)

  • Topic analysis

    Execution order: Promise——>the ​​following (b)——>synchronous (f)

                 .resolve异步的(c)
      
                 .JS执行原理从上到下的(a)
      
                 .then()——>setTimeout异步的(d)
    

4. Please write the ES5 code generated by the following ES6 code compilation
class Person {
     constructor (name) {
          this.name = name;
     }
     greet () {
          console.log(`Hi, my name is ${this.name}`);
     }
     greetDelay (time) {
          setTimeout(() => {
               console.log(`Hi, my name is ${this.name}`);
          }, time);
     }
}
  • Inspection point

    ES6 syntax understanding, JS prototype understanding, understanding of this point

  • Topic analysis

    ES5 uses the JS prototype for output, and the definition of this under the window, which requires the transfer of intermediate variables

    The two functions greet() and greetDelay() are actually two instance function objects under the Person prototype chain


5. A sequence of numbers like 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, the last digit is the addition of the first two digits (Fibonacci sequence), write the function to find the Nth What is the number of bits, such as: fib(3) => 3, fib(5) => 8, requires time complexity of O(n)
 if(n == 0 || n == 1) cout << 1 << endl;
    else{
        long long a = 1, b = 1;
        for(int i=2; i<=n; i++){
            b = a + b;
            a = b - a;
        }
        cout << b << endl;

fib(0)=fib(1)=1; The following number is the sum of the first two numbers, which is equivalent to b is the output of the input value, and the value of a can be changed to the n-1th result by swapping To output; another key point is to give these two variables what type, will there be an overflow problem.


6. The code realizes money conversion. Assuming that there are currently 100 yuan of goods, and there are four vouchers of 50 yuan, 30 yuan, 20 yuan, and 5 yuan, the best offer is two vouchers with a denomination of 50 yuan; There are 65 yuan of goods, the best discount is two 30 yuan voucher and a 5 yuan voucher.
 for(int p=0;p<size;++p){
            for(int q=0;q<size-p-1;++q){
                if(coins[q]>coins[q+1]){
                    swap(coins[q],coins[q+1]);
                }
            }
        }
        int *F=new int[cash+1];
        for(int i=0;i<=cash;++i){
            F[i]=cash+1;
        }
        F[0]=0;
        for(int j=0;j<=cash;++j){
            for(int k=0;k<size;++k){
                if(j<coins[k]){
                    break;
                }
                else{
                    F[j]=min(F[j-coins[k]]+1,F[j]);
                }
            }
        }

The idea of ​​this question is to set the variable calculation from the largest face value, and then select from the smaller face value little by little


7. Given an M x N maze containing non-negative integers, please find a path from the upper left corner to the lower right corner so that the sum of the numbers on the path is the smallest. You can only move one step down or right at a time.
for(int i=0;i<M;i++){
        for(int j=0;j<N;j++){
            cin>>a[i][j];
        }
    }
    for(int i=M-1;i>=0;i--){
        for(int j=N-1;j>=0;j--){
            if(i+1<M && j+1<N)
            {
                a[i][j] += min(a[i+1][j],a[i][j+1]);
            }
            else if(i + 1< M)
            {
                a[i][j] += a[i+1][j];
             
            }else if(j + 1 < N)
            {
                a[i][j] += a[i][j+1];
            }
        }
    }

This question examines a two-dimensional array. First, save the given array in the two-dimensional array, and then make a judgment if you don’t take a step. The lower value is going down and the right part (but still To make a pre-judgment), store the distance traveled in an array for each step, and then display the shortest output distance by adding the contents of the array.

Guess you like

Origin blog.csdn.net/qq_43352105/article/details/110680019