Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Co-Occurrence Counter in Strings
00:00
5 left

Co-Occurrence Counter in Strings

EasyPython

Problem

Tessian Guardian analyzes email text for combinations of terms that may indicate a risky message. Given an email body and two distinct target words, return the number of pairs formed by one occurrence of each target word anywhere in the text.

Matching is case-insensitive. A word is a contiguous sequence of ASCII letters, and punctuation separates words. Each occurrence is counted independently, so if word1 appears twice and word2 appears three times, the result is six.

Formal Specification

Implement count_cooccurrences(text, word1, word2).

  • Input: text, word1, and word2, all strings. The target words contain only ASCII letters and are distinct.
  • Output: An integer representing the number of unordered occurrence pairs containing one occurrence of each target word.

Constraints

  • 1 <= len(text) <= 10^6
  • 1 <= len(word1), len(word2) <= 50
  • word1 and word2 are distinct ASCII-letter strings
  • Matching is case-insensitive
  • Punctuation separates words
  • The result fits in a signed 64-bit integer

Function Signature

def count_cooccurrences(text, word1, word2):
Interviewer

Your question is Co-Occurrence Counter in Strings. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.