cswork / libraryOpen practice workspace

Coding questions · Bloomberg

Count friend requests based on age rules

This question evaluates algorithmic problem-solving skills involving array processing, counting and combinatorics under conditional constraints and frequency handling.

Problem

You are given an array ages, where ages[i] is the age of the i-th person. A person of age A will send a friend request to a person of age B if all conditions hold:

  1. B>0.5×A+7B > 0.5 \times A + 7
  2. B≤AB \le A

Each request is directed (A→B). A person cannot send a request to themselves.

Return the total number of friend requests that will be sent.

Input

  • ages: integer array

Output

  • An integer: total number of directed friend requests.

Constraints (you may assume typical interview bounds)

  • 1≤n≤2×1041 \le n \le 2\times 10^4
  • 1≤ages[i]≤1201 \le ages[i] \le 120

Examples

  1. ages = [16, 16]
  • Each 16-year-old can request the other (since 16>0.5⋅16+7=1516 > 0.5\cdot16 + 7 = 15 and 16≤1616 \le 16)
  • Total = 2
  1. ages = [20, 30, 100, 110, 120]
  • Compute total directed requests that satisfy the rules.

Friend Requests by Age

Count ordered friend requests between people using the standard age rules.

Hints

  1. Start with a direct data structure representation.
  2. Handle edge cases before the main loop.