Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

快手面试题(day6) #22

Open
robbiemie opened this issue Mar 9, 2024 · 4 comments
Open

快手面试题(day6) #22

robbiemie opened this issue Mar 9, 2024 · 4 comments

Comments

@robbiemie
Copy link
Owner

Q1:

var name = '123'
var obj = {
    name: "456",
    getName: function() {
        function printName() {
            console.log(this, this.name) // this->window:123
        }
        printName()
    }
}

obj.getName()

Q2:

var a = 1

function fn() {
    console.log(a)
    var a = 2 // undefined
    // 如果是这个: let a = 2 throw error
}

fn()

Q3:

setTimeout(() => {
  console.log('1') // 宏
}, 0)

new Promise((resolve, reject) => {
  console.log('2') // 同
  process.nextTick(resolve)
}).then(_ => {
  console.log('3') // 微
})

process.nextTick(() => {
  console.log('4') // 微
})

setImmediate(() => {
  console.log(5) // 宏
})

Q4:

/**
 * 实现 repeat 函数
 * @param {Function} 执行函数
 * @param {number} 执行次数
 * @param {number} 执行间隔
 */
const repeatFunc = repeat(console.log, 4, 1000)

repeatFunc('hello')

Q5:

let arr = [
    {a: 1, b: 2},
    {b: 2, a: 1},
    {a:'1', b:2}
]

/**
 * 去重
 * 要求: 时间复杂度尽量最低
*/ 
const newArr = filters(arr)
/**
newArr = [
    {a: 1, b: 2},
    {a:'1', b:2}
]
 */

Q6:

// 实现一个 EventEmit 类
class EventEmit {
    on() {}
    once() {}
    off() {}
    emit() {}
}
@robbiemie
Copy link
Owner Author

/**
 * 实现 repeat 函数
 * @param {Function} 执行函数
 * @param {number} 执行次数
 * @param {number} 执行间隔
 */
const repeatFunc = repeat(console.log, 4, 1000)

repeatFunc('hello')




/**
 * 实现 repeat 函数
 * @param {Function} 执行函数
 * @param {number} 执行次数
 * @param {number} 执行间隔
 */
function repeat(handler, count, wait) {
  let remain = count
  const repeatFunction = function(...args) {
    if(remain === 0) return
    const arg = args[0]
    if(wait === 0) {
      // 立即执行
      handler(arg)
      remain--;
    } else {
      // 间隔执行
      handler.id = setTimeout(() => {
        clearTimeout(handler.id)
        handler(arg)
        remain--
        if(remain>0) {
          repeatFunc(arg)
        }
      }, wait)
    }
  }

  return repeatFunction
}


const repeatFunc = repeat(console.log, 4, 1000)

repeatFunc('hello')

@robbiemie
Copy link
Owner Author

let arr = [
    {a: 1, b: 2},
    {b: 2, a: 1},
    {a:'1', b:2}
]

/**
 * 去重
 * 要求: 时间复杂度尽量最低
*/ 
const newArr = filters(arr)
/**
newArr = [
    {a: 1, b: 2},
    {a:'1', b:2}
]
 */


let arr = [
  {a: 1, b: 2},
  {b: 2, a: 1},
  {a:'1', b:2}
]

function filters(arr) {
  const list = arr.slice()
  let set = new Set()
  for(let item of list) {
    set.add(JSON.stringify({
      a: item['a'],
      b: item['b']
    }))
  }

  return [...set].map(item => JSON.parse(item));
}

/**
* 去重
* 要求: 时间复杂度尽量最低
*/
const newArr = filters(arr)
console.log(newArr)
/**
newArr = [
  {a: 1, b: 2},
  {a:'1', b:2}
]
*/

@robbiemie
Copy link
Owner Author

// 实现一个 EventEmit 类
class EventEmit {
    on() {}
    once() {}
    off() {}
    emit() {}
}


// 实现一个 EventEmit 类
class EventEmit {
  constructor() {
    this.eventMap = {}
  }
  on(name, handler) {
    if(Array.isArray(this.eventMap[name]))  this.eventMap[name] = []
    if(typeof handler === 'function') {
      this.eventMap[name].push(handler)
    }
  }
  once(name, handler) {
    if(Array.isArray(this.eventMap[name]))  this.eventMap[name] = []
    handler.once = true
    if(typeof handler === 'function') {
      this.eventMap[name].push(handler)
    }
  }
  off(name, handler) {
    if(Array.isArray(this.eventMap[name]))  this.eventMap[name] = []
    const queue = this.eventMap[name];
    const index = queue.findIndex(item => item === handler);
    queue.splice(index, 1);
    this.eventMap[name] = queue
  }
  emit(name, ...args) {
    if(Array.isArray(this.eventMap[name]))  this.eventMap[name] = []
    const queue = this.eventMap[name];
    for(let handler of queue) {
      if(typeof handler === 'function') {
        handler(args)
      }
      if(handler.once) {
        handler = null
      }
    }
    this.eventMap[name] = queue.filter(item => item)
  }
}

@robbiemie
Copy link
Owner Author

// linkNode
// [1,2,3]
// [2,4,5]

// [1,2,2,3,4,5]

/**
 * LinkNode
 * @param {number | null} value 
 * @param {LinkNode | null} next
 */

/**
 * 
 * @param {LinkNode} link1 
 * @param {LinkNode} link2 
 */
 function merge(link1, link2) {
    let point = new LinkNode(0)
    let link = point
    // 遍历两个链表
    while(link1 || link2) {
        if(link1 && link2) {
            if(link1.value < link2.value) {
                point.next = link1
                link1 = link1.next
            } else {
                point.next = link2
                link2 = link2.next
            }
            point = point.next
        } else if(link1) {
            // link2 === null
            point.next = link1
            point = point.next
            link1 = link1.next
        } else {
            // link1 === null
            point.next = link2
            point = point.next
            link2 = link2.next
        }
    }
    return link.next
}


merge([], [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant