Solve counting and frequency coding tasks
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given two separate coding tasks.
## Task 1: Count valid “friend requests”
You are given an array `ages[]` of integers representing users’ ages.
A user of age `A` can send a friend request to a user of age `B` **iff** all conditions hold:
- `B <= 0.5 * A + 7` is **false** (i.e., `B` must be strictly greater than `0.5*A + 7`)
- `B > A` is **false** (i.e., `B <= A`)
- If `B > 100`, then `A >= 100` (i.e., users under 100 cannot request users over 100)
Each ordered pair `(i, j)` with `i != j` counts as one request if `ages[i]` can request `ages[j]`.
**Output:** return the total number of valid friend requests.
**Constraints (typical interview scale):**
- `1 <= n <= 2e4`
- `1 <= ages[i] <= 120`
## Task 2: Top-N frequent words from a text source
You are given an input text source (conceptually a file/stream) containing words separated by whitespace and/or punctuation.
**Goal:** return the `N` most frequent words along with their counts.
**Requirements/clarifications:**
- Define how you tokenize words (e.g., case-insensitive, strip punctuation).
- If multiple words have the same frequency, define a deterministic tie-break (e.g., lexicographic order).
- The input may be too large to fit into memory as a single string; assume you can read line-by-line.
**Input:** a stream of text and an integer `N`.
**Output:** a list of up to `N` `(word, count)` pairs sorted by decreasing count (and tie-break rule).
**Constraints (typical interview scale):**
- Total tokens can be large (e.g., millions)
- `1 <= N <= number_of_distinct_words`
Quick Answer: This question evaluates algorithmic skills in counting, frequency analysis, constraint-based pair counting, and stream-oriented tokenization and ranking, and is categorized under Coding & Algorithms for software engineer roles.
Friend Requests by Age
Count ordered friend requests using the age eligibility rules.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: ([16, 16],)
Expected Output: 2
Explanation: Two reciprocal requests.
Input: ([16, 17, 18],)
Expected Output: 2
Explanation: Mixed ages.
Input: ([20, 30, 100, 110, 120],)
Expected Output: 3
Explanation: Over-100 rule.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.
Top N Frequent Words
Return the n most frequent words, sorting ties lexicographically ascending.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: (['i', 'love', 'leetcode', 'i', 'love', 'coding'], 2)
Expected Output: ['i', 'love']
Explanation: Frequency tie.
Input: (['the', 'day', 'is', 'sunny', 'the', 'the', 'the', 'sunny', 'is', 'is'], 4)
Expected Output: ['the', 'is', 'sunny', 'day']
Explanation: Classic example.
Input: (['a', 'b'], 5)
Expected Output: ['a', 'b']
Explanation: n exceeds distinct words.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.