腾讯笔试真题

# 腾讯笔试真题

# 🏢 公司简介

腾讯是中国领先的互联网增值服务提供商,笔试题目注重算法思维、系统架构和产品理解。

# 📝 题目特点

  • 算法广度:涵盖各类经典算法问题
  • 系统设计:考查大型分布式系统设计能力
  • 产品思维:结合微信、QQ等产品场景
  • 代码效率:注重算法的时间和空间复杂度

# 🎯 2024年春招真题

# 题目1:微信群聊消息去重 🟡

题目描述: 微信群聊中可能出现重复消息,设计一个算法来检测和去除重复消息。

输入:

  • 消息列表,每条消息包含:发送者ID、内容、时间戳
  • 相似度阈值threshold

输出:

  • 去重后的消息列表

解题思路:

class Message {
    String senderId;
    String content;
    long timestamp;
    
    public Message(String senderId, String content, long timestamp) {
        this.senderId = senderId;
        this.content = content;
        this.timestamp = timestamp;
    }
}

public List<Message> removeDuplicateMessages(List<Message> messages, double threshold) {
    List<Message> result = new ArrayList<>();
    Set<String> seen = new HashSet<>();
    
    for (Message msg : messages) {
        String key = generateKey(msg);
        if (!seen.contains(key)) {
            boolean isDuplicate = false;
            for (Message existing : result) {
                if (isSimilar(msg, existing, threshold)) {
                    isDuplicate = true;
                    break;
                }
            }
            if (!isDuplicate) {
                result.add(msg);
                seen.add(key);
            }
        }
    }
    
    return result;
}

private String generateKey(Message msg) {
    return msg.senderId + "_" + msg.content.hashCode();
}

private boolean isSimilar(Message msg1, Message msg2, double threshold) {
    // 计算消息相似度
    double similarity = calculateSimilarity(msg1.content, msg2.content);
    return similarity > threshold;
}

# 题目2:QQ好友推荐 🟠

题目描述: 基于用户的好友关系图,为用户推荐可能认识的人。

输入:

  • 用户好友关系图
  • 目标用户ID
  • 推荐数量k

输出:

  • 推荐的k个用户ID

解题思路:

public List<Integer> recommendFriends(Map<Integer, Set<Integer>> friendGraph, 
                                     int userId, int k) {
    Set<Integer> userFriends = friendGraph.getOrDefault(userId, new HashSet<>());
    Map<Integer, Integer> candidates = new HashMap<>();
    
    // 统计二度好友的共同好友数量
    for (int friend : userFriends) {
        Set<Integer> friendsOfFriend = friendGraph.getOrDefault(friend, new HashSet<>());
        for (int candidate : friendsOfFriend) {
            if (candidate != userId && !userFriends.contains(candidate)) {
                candidates.put(candidate, candidates.getOrDefault(candidate, 0) + 1);
            }
        }
    }
    
    // 按共同好友数量排序
    return candidates.entrySet().stream()
            .sorted((a, b) -> b.getValue().compareTo(a.getValue()))
            .limit(k)
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());
}

# 题目3:王者荣耀匹配算法 🔴

题目描述: 设计一个游戏匹配算法,根据玩家的段位、胜率等因素进行公平匹配。

输入:

  • 等待匹配的玩家列表
  • 每个玩家的段位、胜率、等待时间

输出:

  • 匹配成功的队伍组合

解题思路:

class Player {
    int id;
    int rank;      // 段位
    double winRate; // 胜率
    int waitTime;   // 等待时间
    
    public Player(int id, int rank, double winRate, int waitTime) {
        this.id = id;
        this.rank = rank;
        this.winRate = winRate;
        this.waitTime = waitTime;
    }
}

public List<List<Player>> matchPlayers(List<Player> players) {
    List<List<Player>> matches = new ArrayList<>();
    List<Player> available = new ArrayList<>(players);
    
    // 按段位和胜率排序
    available.sort((a, b) -> {
        if (a.rank != b.rank) return Integer.compare(a.rank, b.rank);
        return Double.compare(a.winRate, b.winRate);
    });
    
    while (available.size() >= 10) { // 假设每局需要10个玩家
        List<Player> match = new ArrayList<>();
        
        // 选择段位相近的玩家
        for (int i = 0; i < 10 && i < available.size(); i++) {
            match.add(available.get(i));
        }
        
        // 检查匹配质量
        if (isValidMatch(match)) {
            matches.add(match);
            available.removeAll(match);
        } else {
            break; // 无法找到合适的匹配
        }
    }
    
    return matches;
}

private boolean isValidMatch(List<Player> players) {
    int minRank = players.stream().mapToInt(p -> p.rank).min().orElse(0);
    int maxRank = players.stream().mapToInt(p -> p.rank).max().orElse(0);
    return maxRank - minRank <= 2; // 段位差距不超过2
}

# 🔍 历年高频考点

# 算法类

  • 动态规划(背包问题、最长子序列)
  • 图算法(最短路径、拓扑排序)
  • 字符串匹配(KMP、字典树)
  • 数据结构设计(LRU缓存、一致性哈希)

# 系统设计类

  • 分布式系统设计
  • 缓存策略
  • 负载均衡
  • 数据库分片

# 💡 备考建议

  1. 算法基础:熟练掌握常用数据结构和算法
  2. 系统思维:了解大型互联网系统的架构设计
  3. 产品理解:关注腾讯产品的技术特点
  4. 代码规范:注重代码的可读性和健壮性
  5. 时间管理:合理分配各题的答题时间