百度社招面经(凉凉版)

330 阅读1分钟

百度社招一面:问得比较多的是八股文:

  • 1.http缓存
  • 2.http 1.0 1.1 2.0 区别
  • 3.闭包
  • 4.vue的源码理解
  • 5.性能优化
  • (......等 和一些项目的经验)
  • 算法:大数相加、数组转树形、排序算法之类的

百度社招二面:

  • 1.算法
    • 1.1 100元发10个随机金额的红包
    • 1.2 分词
  • 2.组件库封装、封装一个分页组件

算法

1、100元发10个随机金额的红包

function generateRedPackets(totalAmount,count){
    const packets =[]
    let remainingAmount = totalAmount;
    for(let i=0;i<count-1;i++){
        // 计算平均值
        const avg = remainingAmount / (count - i);
        // 随机生成一个红包金额,范围为【0,01,剩余金额/(剩余数量*2)】
        const max = Math.min(avg * 2, remainingAmount - 0.01 * (count - i - 1));
        // const max = remainingAmount/((count-i)*2)
        const min = avg / 2; // 最小金额为 0.01元
        const amount = Math.random()*(max-min)+min
        const roundedAmount = parseFloat(amount.toFixed(2)) // 保留两位小数
        packets.push(roundedAmount)
        remainingAmount -=roundedAmount
    }
    packets.push(parseFloat(remainingAmount.toFixed(2)))
    return packets
}
const totalAmount = 100 
const count = 10
const redPackets = generateRedPackets(totalAmount,count)
console.log('红包金额',redPackets)
console.log('总金额:', redPackets.reduce((a, b) => a + b, 0));

2、分词算法

function findAdjacentSentences(sentences, targetSentence) {
    // 1. 将数组中的所有字符串拼接成一个完整的字符串
    const fullText = sentences.join('');
  
    // 2. 按句号分割成句子数组,并过滤掉空字符串
    const sentenceArray = fullText.split('。').filter(s => s.trim() !== '');
  
    // 3. 找到目标句子在句子数组中的位置
    const targetIndex = sentenceArray.findIndex(s => s === targetSentence);
  
    if (targetIndex === -1) {
      return "目标句子未找到";
    }
  
    // 4. 获取前一句和后一句
    const prevSentence = targetIndex > 0 ? sentenceArray[targetIndex - 1] + '。' : null;
    const nextSentence = targetIndex < sentenceArray.length - 1 ? sentenceArray[targetIndex + 1] + '。' : null;
  
    return {
      prevSentence,
      nextSentence,
    };
  }
  
  // 示例数据
  const sentences = [
    '现在是5点,',
    '下班去健身。然后吃饭,吃完饭再看一下电视剧。',
    '这样一天就结束,',
    '好快结束了。'
  ];
  
  // 测试
  const targetSentence = '然后吃饭,吃完饭再看一下电视剧';
  const result = findAdjacentSentences(sentences, targetSentence);
  
  console.log(result);

大概是这些,欢迎有人来讨论,指教