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
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
Query on Huge Sparse Matrix
00:00
5 left

Query on Huge Sparse Matrix

HardSQL · PostgreSQL

Problem

Given a large matrix size of 10^18X10^18 and list sub tables with top-left and right bottom coordinates and number filled in those tables. Find the coordinates of number 'n'.

Asked in the Round 1 stage. Write a PostgreSQL query using the provided tables to return the coordinates for every requested number.

Output

  1. One row per matching matrix region and request.
  2. Return request_id, target_number, top_left_row, top_left_col, right_bottom_row, and right_bottom_col.
  3. Exclude requests with no matching region and order by request_id, then top_left_row, top_left_col.

Schema

matrix_subtables
ColumnTypeDescription
subtable_idPKBIGINTUnique identifier for a rectangular matrix region
top_left_rowBIGINTOne-based row of the region's top-left cell
top_left_colBIGINTOne-based column of the region's top-left cell
right_bottom_rowBIGINTOne-based row of the region's bottom-right cell
right_bottom_colBIGINTOne-based column of the region's bottom-right cell
filled_numberBIGINTNumber filling every cell in the region
target_requests
ColumnTypeDescription
request_idPKBIGINTUnique identifier for a lookup request
target_numberBIGINTNumber whose containing regions must be found
Tablesmatrix_subtablestarget_requests
Interviewer

Your question is Query on Huge Sparse Matrix. 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.