data-to-tree-task's Introduction
data-to-tree-task's People
data-to-tree-task's Issues
Solution review
Hi, i'm the one who was running the interview; thanks for sending the complete solution, there might have been some pressure during the call - both as it requires coding on a sort of "white board" in the limited time period, and the fact we had to use English - it's really no-go here w/o the language knowledge, the client team members speak fast west coast American English, and some of asian origin have different accents + it's de-facto the only way to communicate here locally till one gets familiar with intermediate level Polish.
There are still few problems with this solution:
- A minor one - you sort IDs to pass the smallest one, generally you only need the one where parent_id is zero + sorting to find the minimal element is overkill (N*logN vs. just N to scan)
- If you pass some sort of data where a child comes before the parent in the list - which is totally possible if the in-database tree is being edited - you'll find out it doesn't work; try
var input = []Data{
{1, 0, "str1"},
{2, 4, "str2"},
{3, 4, "str3"},
{4, 1, "str4"},
{5, 1, "str5"}}
- The code complexity is at least O(N**2); a faster O(N) solution, which is also must easier to implement is to create a
map[int]*InMemory
- indexed byID
with empty children - and then scan this map again adding each scanned element tothatMap[element.ParentID].Children
, finally finding the one withParentID == 0
and returning it. - An extra question is usually - is it possible to create some loop in such an in-database tree and how to make the code detect such sort of loop.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.