科技公司 SWE 在线评估:双指针与滑动窗口从头讲清
SWE Online Assessment: Two Pointers and Sliding Window, From Scratch
维护站点的编辑标准、披露规则,以及归档和活跃内容的修订流程。
摘要 Summary
双指针和滑动窗口,能把很多 O(n^2) 的暴力想法压成一次 O(n) 扫描。这篇文章讲你真正会遇到的三种形态——两端相向、快慢指针、可伸缩窗口,并用一个大家最容易卡住的可变长度窗口例子把它写清楚。
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.
双指针和滑动窗口本质上是同一个想法:不去枚举每一对元素或每一个子数组,而是让一两个下标在数据上走一遍,边走边只维护刚好够用的状态。就是这一点改变,把 O(n^2) 的暴力变成了 O(n) 的一遍扫描,也是为什么这类题大约每六道就会出现一道。
这篇文章整理自公开、广为人知的算法题型,而不是某家公司的真实原题。把它当成一张练习地图:先记住每种题的识别信号,每个模式手里留一份干净的参考解法,剩下的时间用来把题读懂,读到能一眼选对套路。
你真正会遇到的三种形态| The Three Shapes You Actually See
两端相向:一个指针从左端、一个从右端,往中间靠拢——常见于有序数组里的两数之和、回文判断。
快慢指针:两个指针以不同速度走同一个序列——用来判环、找中点。
滑动窗口:左右两个边界圈出一个窗口,右边界扩张纳入新元素,约束被破坏时左边界收缩。
固定窗口与可变窗口| Fixed Versus Variable Windows
固定窗口的长度 k 是已知的:右边加入新元素,左边移出滑出去的那个,每一步读一次答案。
可变窗口先一直扩张,直到违反某个条件,再从左边收缩,直到条件重新成立。
大多数人卡住的正是可变窗口,因为「什么时候收缩」和「什么时候记录答案」这两步都必须踩得很准。
一个例子:无重复字符的最长子串| A Worked Example: Longest Substring Without Repeats
这是可变窗口的经典题。右边界每次向右吃进一个字符;一旦这个新字符已经在窗口里,就从左边收缩,直到它不再重复。每一步结束时窗口总是合法的,所以可以放心地记录它的长度。
def longest_unique_substring(s):
seen = {} # char -> most recent index
left = 0
best = 0
for right, ch in enumerate(s):
# if ch is already in the window, jump left past its last position
if ch in seen and seen[ch] >= left:
left = seen[ch] + 1
seen[ch] = right
best = max(best, right - left + 1)
return best注意 seen[ch] >= left 这个判断。少了它,一个来自当前窗口之前的旧下标会把左指针往回拉,把答案弄坏。正是这种小条件,面试官盯得最紧,所以动手前先把不变量说出来——「left 到 right 之间的字符互不相同」。
最容易失分的几个点| Common Mistakes That Cost Marks
把左指针往回移,或者忘了旧下标可能已经落在当前窗口之外。
在错误的时刻记录答案——在收缩之前就记,而不是等窗口重新合法之后。
出于习惯写了两层嵌套循环,其实一次窗口扫描就是 O(n)。
做两端相向的题时,忘了两数之和的逻辑成立的前提是数组已经有序。
一个够用的练习计划| A Focused Practice Plan
把一道固定窗口题和一道可变窗口题连着做,真正体会两者的区别。
每道窗口题,都在循环前用注释写下不变量。
练习用嘴说清楚:窗口什么时候扩张、什么时候收缩。
把快慢指针判环的思路从头自己推一遍,别只当成一个背下来的技巧。
常见问题 FAQ
什么时候双指针比哈希表更合适?
When is two pointers better than a hash map?
当输入已经有序、或者排序代价很小时,双指针常常能以 O(1) 额外空间解决配对和区间问题;而哈希表是用内存换「不需要有序」。
怎么判断窗口该用固定长度还是可变长度?
How do I know whether a window should be fixed or variable?
如果题目直接给了长度 k,就是固定窗口。如果问的是满足某个条件的最长或最短窗口,就是可变窗口,用先扩张后收缩的写法。
为什么我的滑动窗口在边界用例上会算错?
Why does my sliding window give the wrong answer on edge cases?
通常是收缩条件或记录答案的时机差了一。回头确认:在你读取窗口大小的那一刻,窗口是不是合法的。
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: Array and String Problems Made Approachable
科技公司 SWE 在线评估:数组与字符串题该怎么拆
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.
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.