Wish 前端店面挂经

猎头联系了我,并安排了一个小时的技术电话轮。
面试官上了介绍了自己,并谈了一些他的工作。然后轮到我,我解释了我做了什么,以及我的技术栈。
15分钟后用codepair写码。

第一题
Given a JSON object, write a function that converts it into a string. For example,

const json = {
	value: "test"
}

return this - “test” [includes HTML body tag surrounding test but that is not getting displayed here];
I asked if he meant to return dom elements from the string but he wanted string only. I wrote a recursive function solution that checked if json was object, array or string and create element accordingly, with the values - but he did not seem convinced. He kept on asking me to return a string that looked like above.

第二题 https://leetcode.com/problems/min-stack/

我的代码

class Stack {
  constructor() {
    this.items = [];
    this.minEl = 0;
  }

  push(item) {
    if (this.items.length === 0) {
      this.minEl = item;
      this.items.push(item);
      return;
    }
    if (item < this.minEl) {
      this.items.push(2 * item - this.minEl);
      this.minEl = item;
    } else {
      this.items.push(item);
    }
  }

  pop() {
    if(this.items.length === 0) {
      return;
    }
    
    let temp = this.items.pop(); 
    if(temp < this.minEl) {
      this.minEl = 2*this.minEl - temp; 
    } else {
      return temp;
    }
  }

  getMin() {
    return this.minEl;
  }
}

const stack = new Stack();

stack.push(4);
stack.push(5);
console.log(stack.getMin()); //4

stack.push(6);
stack.push(1);

console.log("round 2",stack.getMin());  // 1

没问 Leetcode Wish tagged 的题. 第一题是 web dev related. 一小时后收到据信。