Skip to content
返回博客 Back to Blog
面试与评估中心 Interviews & Assessment Centres

科技公司 SWE 在线评估:哈希表与集合题型精讲

SWE Online Assessment: Hash Map and Set Patterns

8 min read

维护站点的编辑标准、披露规则,以及归档和活跃内容的修订流程。

摘要 Summary

哈希表和集合能换来平均 O(1) 的查找,而在线评估里相当多的题,本质上就是在考你「用什么当 key」。这篇文章讲清补数法、频次表、按签名分组,以及用一个 seen 集合在一遍扫描里发现重复。

Hash maps and sets buy you O(1) average lookups, and a surprising number of assessment problems are really about choosing the right key. This guide covers the complement trick, frequency maps, grouping by a signature, and using a seen-set to detect duplicates in one pass.

哈希表或集合能提供平均 O(1) 的插入和查找,而在线评估里很大一部分题,真正考的其实是一个决定:key 该选什么?一旦选对了 key——某个值、某个计数、或某种归一化后的签名——剩下的解法往往就是一遍扫描。所以这一类题虽然占比不大,却很值得练熟。

这篇文章整理自公开、广为人知的算法题型,而不是某家公司的真实原题。把它当成一张练习地图:先记住每种题的识别信号,每个模式手里留一份干净的参考解法,剩下的时间用来把题读懂,读到能一眼选对套路。

值得记住的几个模式| The Patterns Worth Knowing

  • 补数法:一边扫描,一边查「我还差的那个值」是不是已经见过——这是两数之和这一类题的核心。

  • 频次表:统计出现次数,再回答关于出现最多、最少或恰好 k 次的问题。

  • 按签名分组:把每个元素映射到一个规范化的 key(比如排序后的字母),让等价的元素归到一起。

  • seen 集合去重:维护一个「已见过」的集合,一遍扫描就能发现第一个重复,或过滤掉重复项。

怎么一眼认出是哪一种| How to Recognise Each One

  • 「找到两个加起来等于目标的东西」,几乎总是补数法。

  • 「分组」「变位词」「在某种规则下等价的元素」,指向按签名分组。

  • 「第一个重复」「是否含重复」「唯一」,就是 seen 集合。

  • 只要涉及「出现最多」「前 k 个」,都从频次表起步。

一个例子:一遍扫描的两数之和| A Worked Example: Two Sum in One Pass

暴力做法要检查每一对,是 O(n^2)。哈希表版本只扫一遍:对每个数,问它的补数(target 减去这个数)是不是已经出现过。出现过就得到答案;没有就把当前数记下来,继续走。

python
def two_sum(nums, target):
    seen = {}                 # value -> index
    for i, value in enumerate(nums):
        need = target - value
        if need in seen:      # complement already passed by
            return [seen[need], i]
        seen[value] = i       # record only after checking
    return []

顺序很关键:先查补数,再插入当前值,否则单个元素可能会和自己配对。正是这种一行的顺序细节,决定了你的解法是全部通过,还是在某个隐蔽用例上翻车,所以要讲清楚为什么插入要放在最后。

最容易失分的几个点| Common Mistakes That Cost Marks

  • 在查补数之前就插入当前值,导致元素和自己配对。

  • 本该用集合(O(1))却用了列表的 in 判断(O(n)),悄悄把解法退回 O(n^2)。

  • 选了可变或不稳定的 key,比如用列表,而不是元组或归一化后的字符串。

  • 忘了哈希是用内存换速度——在内存受限时,要主动说明这多出来的 O(n) 空间。

一个够用的练习计划| A Focused Practice Plan

  • 每道题先定 key,并在写别的之前先用注释写下来。

  • 把补数法和按签名分组各练到形成条件反射。

  • 每次想用哈希表时,先问一句:普通数组下标或集合是不是更简单。

  • 把时间和空间成本说出来,因为「内存换速度」是很常见的追问。

常见问题 FAQ

哈希表一定是最快的选择吗?

Is a hash map always the fastest choice?

不一定。当 key 是较小的整数时,用值直接做下标的普通数组更快、更省内存。key 空间很大或非数值时,才用哈希表。

为什么两数之和遇到重复数字会出错?

Why does two sum fail when there are duplicate numbers?

通常是因为你在查补数之前就插入了当前值。先查、后插,重复数字就能正确处理。

分组类问题该用什么当 key?

What key should I use for grouping problems?

用一个对所有等价元素都相同的规范形式——变位词用排序后的字符,或者一个计数元组。key 必须是不可变、可哈希的。

How This Article Was Produced

来源、审核和披露说明

Source Type
Editorially reviewed guidance built from public source material and manual synthesis.
基于公开资料和人工综合整理,并经过编辑复核的指南内容。
AI Use
AI-assisted drafting was used to organise notes and bilingual copy. A human editor reviewed structure, claims, and final wording before publication.
AI 用于整理笔记和辅助双语表达。正式发布前,文章结构、关键判断和最终措辞均由人工编辑复核。
Author
UK Career Wiki Editorial Team
Editorial review and publishing standards
Reviewed By
UK Career Wiki Editorial Team
Last reviewed on 16 July 2026
What This Page Adds
We condensed the material into decision-first guidance and surfaced the questions readers should answer before acting on the advice.
我们把资料压缩成以决策为先的指南,并补出了读者在采取行动前最该回答的关键问题。

For more on sourcing, corrections, and our recovery workflow, see Editorial Policy and Contact.

专题延伸阅读 Related Guides

按岗位方向、主题关键词和发布时间推荐,帮助读者从单篇文章进入完整准备路径。

面试与评估中心

Python Interview Prep for Data Science and Analytics: Solve the Common Patterns Cleanly

数据科学与分析岗 Python 面试:高频题不用炫技,先把常见模式写干净

Python interviews for data science and analytics roles usually focus on data handling, transformation, simple statistics, and whether you can explain your code. This guide groups the common challenge types and shows how to solve them cleanly without turning a straightforward task into something fragile.

面试与评估中心

Data Engineer Interview Guide: The Questions Behind SQL, Pipelines, and Reliability

数据工程师面试指南:SQL、管道和稳定性背后真正会被问什么

Data engineer interviews often look like a wall of unrelated questions until you group them by the decisions engineers actually make. This guide focuses on data modelling, pipeline design, operational reliability, and the judgement signals that separate a merely technical answer from a good engineering answer.