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
Palindrome Path Queries on Rooted Tree
00:00
5 left

Palindrome Path Queries on Rooted Tree

HardSQL · PostgreSQL

Problem

Given a rooted tree whose edges are labelled with lowercase letters, answer Q queries: does the path from the root to the given node form a palindrome? Target O(N+Q). Asked in the DSA round stage. The interviewer expected an O(N+Q) solution. Use the tree_nodes and palindrome_queries tables.

Output

  1. Return one row per query with query_id, node_id, and is_palindrome.
  2. Include only queried nodes reachable from the root.
  3. Order by query_id ascending.

Schema

tree_nodes
ColumnTypeDescription
node_idPKINTUnique identifier of the tree node
parent_idINTParent node identifier, or NULL for the root
edge_labelVARCHAR(1)Lowercase label on the edge from the parent, or NULL for the root
palindrome_queries
ColumnTypeDescription
query_idPKINTUnique identifier of the query
node_idINTNode whose root path must be checked
Tablestree_nodespalindrome_queries
Interviewer

Your question is Palindrome Path Queries on Rooted Tree. 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.