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
Start free
Hash Table Operations Tradeoff
00:00
5 left

Hash Table Operations Tradeoff

HardSQL · PostgreSQL

Problem

Build a hash table where getRandom(), put() and get() are O(1) and delete is O(n).

Asked in the phone interview stage. Implement the structure using PostgreSQL tables and functions, then show its final contents after the prescribed operations.

Output

  1. Return one row per remaining entry with columns slot, key, and value.
  2. Include entries after updating account_003 and deleting account_006.
  3. Order by slot ascending.

Output details

  • Initialize slots 1 through 10 with keys account_001 through account_010 and values active, pending, active, active, NULL, suspended, active, pending, active, closed, respectively.
  • Set account_003's value to suspended before producing the output.
  • Preserve account_005's value as SQL NULL.
  • After deleting account_006, shift every later entry one slot earlier so slots remain consecutive.

Schema

hash_table_entries
ColumnTypeDescription
slotPKBIGINTCompact position of the entry
keyVARCHAR(64)Unique hash-table key
valueVARCHAR(32)Value associated with the key
Tableshash_table_entries
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.