Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:33:05+00:00 2026-05-27T11:33:05+00:00

I was given this question at a job interview recently and couldn’t figure out

  • 0

I was given this question at a job interview recently and couldn’t figure out how to do it elegantly. Ever since, it has been nagging away at me and I can’t work out if its a lack of knowledge about some ‘modern’ technique/technology I’m unaware of or if I’m just stupid. Any advice would be very welcome.

The Problem

Imagine a simple class hierarchy:

abstract class Person {
    public string Name { get; set; }
}

class Child : Person { }

class Parent : Person {
    public List<Person> Children { get; set; }
}

class Ancestor : Parent { }

The problem is how to traverse a hierarchy of such objects and to print out all the people encountered. So for the following scenario:

Ancestor myAncestor = new Ancestor {    
    Name = "GrandDad",
    Children = new List<Person> { 
        new Child { Name = "Aunt" },
        new Child { Name = "Uncle" },
        new Parent {
            Name = "Dad", 
            Children = new List<Person> { 
                new Child { Name = "Me" }, 
                new Child { Name = "Sister" } 
            }
        }
    }
};

the output should be something like:

GrandDad  
-    Aunt  
-    Uncle  
-    *Dad  
         -Me  
         -Sister

All the processing needs to be done within a single method that accepts a single parameter of type Ancestor.

I implemented, almost without thinking, a simple recursive solution but of course because of the way the objects involved relate to each other things aren’t as simple as all that.

Try as I might I cannot think of a clean way of doing this and my post-interview Googlings have suggested I need to be doing something that is (to me, with only a working knowledge of LINQ and List<T>) something considerably more technically advanced than the sort of web-dev coding I’ve been doing for the last decade or so. Is this the case? Or should I be thinking of getting out of software development on the grounds that I’m rubbish at it?

Update

Thanks to you all for your responses/suggestions. I’ve accepted @Daniel Hilgarth’s answer primarily because it was the only one I could genuinely understand 😮

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:33:06+00:00Added an answer on May 27, 2026 at 11:33 am

    I agree with Marc’s comment saying that this type system is non-sense. Still, you can solve it with delegates. That’s a bit of cheating, because basically they are nothing more than methods, but here we go:

    void PrintFamily(Ancestor a)
    {
        Action<Parent, int> printParent = null;
        printParent = (parent, level) => 
        {
            var indentation = new string(' ', level * 4);
            var indentationChildren = new string(' ', (level + 1) * 4);
            Console.WriteLine(indentation + parent.Name);
            foreach(var child in parent.Children)
            {
                if(child is Child)
                    Console.WriteLine(indentationChildren + child.Name);
                else if(child is Parent)
                {
                    printParent((Parent)child, level + 1);
                }
            }
        };
    
        printParent(a, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I went to a job interview today and was given this interesting question. Besides
This is a question that I was asked on a job interview some time
I know this question has been asked before and I read all the answers
This was an interview question. Given Visual Studio 2008 and an icon saved as
Recently in a job interview, I was given the following problem. Say I have
This is the best way I can think of phrasing this question, given this
In this question , I was given a really cool answer to alternating an
This question asks how to compute the Cartesian product of a given number of
I came across this question: say given two weights 1 and 3, u can
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.