科技公司 SWE 在线评估:数组与字符串题该怎么拆
SWE Online Assessment: Array and String Problems Made Approachable
维护站点的编辑标准、披露规则,以及归档和活跃内容的修订流程。
摘要 Summary
数组和字符串题是软件工程师在线评估(OA)里出现频率最高的一类。这篇文章不堆题目,而是把大多数题背后的那几个模式讲清楚:频次统计、前缀和、原地修改和单遍解析,帮你在限时里稳稳写出干净又正确的解法。
Array and string questions are the single most common category in software-engineer online assessments. This guide explains the few patterns behind most of them — frequency counting, prefix sums, in-place editing, and single-pass parsing — and how to reach a clean, correct solution under time pressure.
对大多数软件工程师的在线评估来说,数组和字符串题是最容易拉开时间差的地方。好消息是这一类其实很窄:很大一部分题目最后都能归到几个套路——统计频次、构造前缀和、原地修改数组,或者一遍扫描解析字符串。只要能认出眼前是哪一种,通常就不需要什么花哨技巧。
这篇文章整理自公开、广为人知的算法题型,而不是某家公司的真实原题。把它当成一张练习地图:先记住每种题的识别信号,每个模式手里留一份干净的参考解法,剩下的时间用来把题读懂,读到能一眼选对套路。
大多数题背后的几个模式| The Patterns Behind Most Questions
频次统计:先数清每个值或字符出现多少次,再基于这些计数回答问题。
前缀和:预先算好累积和,之后任意区间和都能用一次减法在 O(1) 得到。
原地修改:用一个读指针和一个写指针在原数组上重排或过滤,而不额外开空间。
单遍解析:一遍走过字符串,同时只维护少量状态,比如当前数字、当前 token 或括号深度。
怎么一眼认出是哪一种| How to Recognise Each One
出现「多少个」「出现最多」「重复」这类字眼,通常指向一个计数用的哈希表。
「子数组之和」或者两个下标之间的「区间」,几乎就是前缀和的信号。
「不要用额外空间」「原地修改」,是在要你用双指针原地处理。
只要涉及解析表达式、版本号或有格式的文本,基本就是一台单遍状态机。
一个例子:用前缀和回答区间求和| A Worked Example: Range Sums With a Prefix Array
假设题目要你回答一个固定数组里很多个不同区间的和。每次直接重算是每个查询 O(n)。而一个前缀和数组能把每次查询压成一次减法——当数据量一大,这就是「能过」和「超时」的区别。
def range_sum_queries(nums, queries):
# prefix[i] holds the sum of nums[0:i], so prefix[0] == 0
prefix = [0] * (len(nums) + 1)
for i, value in enumerate(nums):
prefix[i + 1] = prefix[i] + value
# sum of nums[left..right] is a single O(1) subtraction
results = []
for left, right in queries:
results.append(prefix[right + 1] - prefix[left])
return results这里真正的关键不是这段代码,而是在写循环之前先说清楚 prefix[i] 到底代表什么,并把边界对齐,让「空前缀」落在下标 0。判题系统扣你分最多的往往是差一(off-by-one),而不是慢一点点的写法。
最容易失分的几个点| Common Mistakes That Cost Marks
明明一遍扫加一个计数器就够,却先去排序,白白付出 O(n log n)。
题目明确要求原地处理,你却又开了个新数组。
在字符串不可变的语言里当成可变对象直接改,而不是先转成列表或用 builder。
忘了空输入、只有一个元素、或全部相同这些情况——恰恰是判题系统最先测的。
一个够用的练习计划| A Focused Practice Plan
分别用一小段时间专攻计数、前缀和、原地双指针和解析,每种留一份参考解法。
每道练习题动手前,先开口说清楚它属于哪种模式、为什么。
做错的题,从空白重写一遍,而不是回去再看答案。
做几次限时练习,让「认真读题」在计时状态下依然是自然反应。
常见问题 FAQ
数组和字符串题在 SWE 在线评估里占比有多大?
How much of a software-engineer OA is array and string questions?
它通常是占比最大的一类,常常接近三分之一。把这几个核心模式练到又快又稳,比去追那些冷门题型划算得多。
我该背题解还是背模式?
Should I memorise solutions or patterns?
背模式。背下来的题解一旦换个说法就崩,但真正理解的模式(比如用前缀和处理区间查询)能迁移到很多题上。
怎样才能最有效地避免差一错误?
What is the fastest way to avoid off-by-one errors?
在写循环前先写清楚每个下标代表什么,提交前先测最小的几个用例——空、单元素、以及边界下标。
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 用于整理笔记和辅助双语表达。正式发布前,文章结构、关键判断和最终措辞均由人工编辑复核。
- 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
按岗位方向、主题关键词和发布时间推荐,帮助读者从单篇文章进入完整准备路径。
SWE Online Assessment: Two Pointers and Sliding Window, From Scratch
科技公司 SWE 在线评估:双指针与滑动窗口从头讲清
Two pointers and sliding window turn many O(n^2) brute-force ideas into a single O(n) pass. This guide covers the three shapes you actually see — opposite ends, fast and slow, and a growing/shrinking window — with a clean example of the variable-size window that trips people up most.
SWE Online Assessment: Hash Map and Set Patterns
科技公司 SWE 在线评估:哈希表与集合题型精讲
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.
SWE Online Assessment: Graph and Tree Traversal Without the Panic
科技公司 SWE 在线评估:图与树的遍历不再慌
Graph and tree questions look intimidating but reduce to a small toolkit: BFS for shortest steps in an unweighted graph, DFS for exploring or counting connected regions, and level-order traversal for trees. This guide explains when to use each and a clean grid example most people can adapt.
SWE Online Assessment: Dynamic Programming You Can Actually Derive
科技公司 SWE 在线评估:能自己推出来的动态规划
Dynamic programming feels hard because people memorise solutions instead of deriving them. This guide gives a repeatable four-step method — define the state, write the transition, set the base case, choose an order — and applies it to a clean climbing/coin example you can reuse.
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.