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