Can any one shed light on how to solve this tree question? I was asked during my Facebook phone interview. Given a tree, the algorithm should print node values in a certain order.
______________________ A
_______________ /________________\
________ B__________________________ C
_____/_______\_____________________/______\
__D__________(null)___________E___________F
It should print
ABD
ACE
ACF
I came up with a solution to do that and went through each line of my code with the above input node values and verified that the code produces the desired output.
After that, the interview added one more node as shown below, and said the output should be as shown below. The thing I had a difficulty with was the K value on its own on the last line while the other lines all start with root node value A. I struggled for trying to come up with an algorithm that would leave only K on the 4th iteration(pass) about several minutes and I ran out of time.
______________________ A
_______________ /________________\
________ B____________________________C
_____/_______\_____________________/_______\
___D__________(null)_____________E_____________F
_/_____\______________________/_____\__________/_____\
(null)__(null)_______________(null)___(null)__(null)_______K
should print
ABD
ACE
ACF
K
Want to see the real deal?
More inside scoop? View in App
More inside scoop? View in App
blind
SUPPORT
FOLLOW US
DOWNLOAD THE APP:
FOLLOWING
Industries
Job Groups
- Software Engineering
- Product Management
- Information Technology
- Data Science & Analytics
- Management Consulting
- Hardware Engineering
- Design
- Sales
- Security
- Investment Banking & Sell Side
- Marketing
- Private Equity & Buy Side
- Corporate Finance
- Supply Chain
- Business Development
- Human Resources
- Operations
- Legal
- Admin
- Customer Service
- Communications
Return to Office
Work From Home
COVID-19
Layoffs
Investments & Money
Work Visa
Housing
Referrals
Job Openings
Startups
Office Life
Mental Health
HR Issues
Blockchain & Crypto
Fitness & Nutrition
Travel
Health Care & Insurance
Tax
Hobbies & Entertainment
Working Parents
Food & Dining
IPO
Side Jobs
Show more
SUPPORT
FOLLOW US
DOWNLOAD THE APP:
comments
So imagine that each time you hit a leaf node, you print the entire route so far, wrapping at 3 characters. Meaning something like
temp_route = route
while temp_route.length > 3:
....print(temp_route[:3])
....temp_route = temp_route[3:]
I guess since it was easy they made little harder with the K.