Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Tournament Win Counts
00:00
5 left

Tournament Win Counts

HardSQL · PostgreSQL

Problem

Given a tournament of m games, each played between two players with a known winner and margin, answer queries of the form: given two players, who won more matches? Asked in the pair programming round 2 stage. Part 1 is a simple hashmap solution returning -1 on a tie. Part 2 breaks ties transitively: if A and B never played but A beat C and C beat B, A wins, requiring DFS from both sides. Reported follow-ups: On a tie, resolve transitively via DFS from both players.

Use the games and match_queries tables for Part 1.

Output

  1. One row per query, with query_id, player_a, player_b, and result.
  2. Return 1 when player_a has more wins, 2 when player_b has more wins, and -1 on a tie.
  3. Order by query_id ascending.

Schema

games
ColumnTypeDescription
game_idPKINTUnique game identifier
player_oneINTIdentifier of the first player
player_twoINTIdentifier of the second player
winnerINTIdentifier of the game winner
marginINTWinning margin
match_queries
ColumnTypeDescription
query_idPKINTUnique comparison query identifier
player_aINTFirst player being compared
player_bINTSecond player being compared
Tablesgamesmatch_queries
Interviewer

Your question is Tournament Win Counts. Start with the requirements and the two tables 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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.