Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
Experiences
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
Build my plan
Combinatorics in Python
00:00
5 left

Combinatorics in Python

MediumPython

Problem

Citadel Securities analytics may need to count possible order arrangements and selections without enumerating them. Implement a function that returns both the number of ordered arrangements and unordered selections of r items chosen from n distinct items.

Formal Specification

Implement count_permutations_and_combinations(n, r):

  1. Return [P(n, r), C(n, r)].
  2. P(n, r) = n! / (n-r)!.
  3. C(n, r) = n! / (r!(n-r)!).
  4. Use exact integer arithmetic. Do not generate the arrangements or selections.
  5. The inputs satisfy 0 <= r <= n, so the function must also handle r = 0.

Example 1: n = 5, r = 2 returns [20, 10]. There are 5 choices for the first ordered position and 4 for the second, while unordered selections divide those 20 arrangements by 2!.

Example 2: n = 6, r = 0 returns [1, 1]. There is exactly one empty arrangement and one empty selection.

Constraints

  • 0 <= r <= n
  • 0 <= n <= 1000
  • Results may exceed 64-bit integer range
  • Inputs are integers

Function Signature

def count_permutations_and_combinations(n, r):
Interviewer

Your question is Combinatorics in Python. 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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.