Meta VO 面试真题解析:Subarray Sum Equals K、二叉树访问时间、最长假期规划

18次阅读
没有评论

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals k.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,1,1], k = 2
Output: 2

Example 2:

Input: nums = [1,2,3], k = 3
Output: 2

Given a tree, how long will it take to visit all X nodes optimally?

Traversing one edge takes 1 unit of time. You must return back to the root (Start).

In the tree above, the answer is 6.

Given a calendar year represented as a char array that contains either H or W, where:

  • H = Holiday
  • W = Workday

Given a number of Personal Time-Off days (PTO), maximize the length of the longest vacation you can take.

Example:

[W, H, H, W, W, H, W], PTO = 2
Your maximum vacation is 5 days.

这组 Meta VO 面试题主要考察三类经典思路:第一题是前缀和 + 哈希表统计子数组和等于 k 的个数,核心是把“以当前位置结尾”的子数组和转化为历史前缀和差值;第二题是树上遍历代价问题,通常要抓住“访问所有目标节点并回到根节点”这一约束,答案往往与需要覆盖的边数成倍相关;第三题则是连续假期最大化,属于滑动窗口 / 双指针的典型应用,可以把 PTO 视为可补上的缺口,维护窗口内工作日数量不超过限制,从而求出最长连续可休假的区间。

正文完
 0