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
Graph Construction and Math
00:00
5 left

Graph Construction and Math

MediumPython

Problem

Given N(total nodes) and two arrays consisting the edges, for each acyclic graph, add nodes in graph, calculate the square root and add all square roots.

Asked in the HackerRank OA stage. Treat nodes as labeled 1 through N; edge_u[i] and edge_v[i] form one undirected edge. The edges form a forest. Return the sum of the square roots of the node-label totals for all connected components.

Input and Output

Implement sum_component_square_roots(n, edge_u, edge_v). Return a floating-point value. Nodes with no incident edge form single-node components.

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(edge_u) = len(edge_v) <= n - 1
  • Every endpoint is in [1, n]
  • The undirected graph is acyclic

Function Signature

def sum_component_square_roots(n, edge_u, edge_v):
Interviewer

Your question is Graph Construction and Math. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.