KatInterview

Process Morse Signals and Provider Coverage

Dynamic ProgrammingAuto-graded · 13 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingstringsarrays

Last updated

Evaluate string encoding, ambiguous Morse code decoding via dynamic programming, and spatial coverage checking for network members.

You are tasked with building an analytics processor that handles two separate utility functions in a single batch processing routine:

  1. Morse Code Translation & Ambiguous Signal Parsing

    • Encoding: Translate a plain text string (containing lowercase English letters 'a' through 'z') into its concatenated International Morse Code signal (without spaces or separators).
    • Decoding: Given an unsegmented sequence of Morse symbols ('.' and '-'), compute all possible valid plaintext word combinations of lowercase English letters that could yield this exact Morse pattern.

    Standard International Morse mapping:

    • a: .-, b: -..., c: -.-., d: -.., e: ., f: ..-., g: --., h: ...., i: .., j: .---, k: -.-, l: .-.., m: --, n: -.., o: ---, p: .--., q: --.-, r: .-., s: ..., t: -, u: ..-, v: ...-, w: .--, x: -..-, y: -.--, z: --..
  2. Healthcare Provider Network Access Analysis

    • You are given a network configuration consisting of a list of service providers, a list of plan members, and a maximum allowed radial reach distance max_distance.
    • Each provider is represented by a dictionary: {"id": int, "specialty": str, "location": [x, y]}.
    • Each member is represented by a dictionary: {"id": int, "location": [x, y], "required_specialties": [str, ...]}.
    • A member has adequate coverage if and only if, for every specialty listed in their required_specialties, there exists at least one provider offering that specialty located within Euclidean distance $\le$ max_distance from the member's coordinates.
    • Find all members who fail to achieve adequate coverage.

Return a dictionary with three key-value pairs:

  • "encoded": A str representing the Morse signal of text.
  • "possible_texts": A List[str] containing all valid word decodings of morse, sorted in lexicographical order.
  • "inadequate_member_ids": A List[int] containing the IDs of all members with inadequate access, preserving the relative order in which they appear in members.

Constraints

  • text consists only of lowercase English letters ('a' - 'z').
  • morse consists only of characters '.' and '-'.
  • All location coordinates are 2D pairs of numbers [x, y].
  • Euclidean distance is measured as $\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. Distances $\le$ max_distance are considered covered.
  • Duplicate entries or zero required specialties should be handled according to standard logic (a member with zero required specialties is adequately covered).

Examples

Example 1

Input: solve_morse_and_network_access('gin', '--...-.', [{'id': 1, 'specialty': 'Cardiology', 'location': [1, 2]}, {'id': 2, 'specialty': 'Dermatology', 'location': [3, 4]}], [{'id': 101, 'location': [2, 3], 'required_specialties': ['Cardiology', 'Dermatology']}, {'id': 102, 'location': [10, 10], 'required_specialties': ['Cardiology']}], 5)

Output: {'encoded': '--...-.', 'possible_texts': ['geae', 'geen', 'geete', 'ger', 'gf', 'gin', 'gite', 'gue', 'meeae', 'meeen', 'meeete', 'meer', 'mef', 'mein', 'meite', 'meue', 'miae', 'mien', 'miete', 'mir', 'msn', 'mste', 'mve', 'tbn', 'tbte', 'tdae', 'tden', 'tdete', 'tdr', 'tneae', 'tneen', 'tneete', 'tner', 'tnf', 'tnin', 'tnite', 'tnue', 'tteeae', 'tteeen', 'tteeete', 'tteer', 'ttef', 'ttein', 'tteite', 'tteue', 'ttiae', 'ttien', 'ttiete', 'ttir', 'ttsn', 'ttste', 'ttve', 'zae', 'zen', 'zete', 'zr'], 'inadequate_member_ids': [102]}

Hints

Hint 1

For Morse encoding, map each character in text to its standard representation and join them into a single string.

Hint 2

To decode the unsegmented Morse string, use dynamic programming or memoized recursion starting from the end of the string. At each index i, try matching every alphabet letter's code as a prefix of morse[i:].

Hint 3

To compute spatial distance safely without float precision issues, compare the squared Euclidean distance directly with max_distance * max_distance.

Hint 4

Group provider locations by specialty in a dictionary mapping specialty -> list of (x, y) coordinates for fast lookup during coverage verification.