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

  • Home
  • SEARCH
  • 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 6672945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:34:06+00:00 2026-05-26T03:34:06+00:00

This is an interview question that I found interesting. Write a method that takes

  • 0

This is an interview question that I found interesting.

Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed-in data structure.

The Node structure contains two pointers to other Node structures.
For example, the method signature could look like so:

Node* Copy(Node* root);

Note – Do not make any assumptions about the data structure – it could be a tree, linked list, graph, etc.

How can this be done for any data structure ?

  • 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-26T03:34:07+00:00Added an answer on May 26, 2026 at 3:34 am

    In the generic graph case, you need a mapping from nodes in the original graph to nodes in the new graph, so that when a cycle is encountered, the proper link gets created. If you happen to have extra temporary space in each node, large enough to hold a pointer, then you can store the mapping directly in the nodes; otherwise, you’ll need to use an external map, such as an associative array or hash table.

    Then it’s just a matter of traversing the graph, copying nodes, and looking up the corresponding edges. Something like this:

    struct Node
    {
        Node(int _data) : data(_data) { memset(links, 0, sizeof(links)); }
    
        int data;
        Node *links[2];
    }
    
    Node *Copy(Node *root)
    {
        typedef std::map<Node*, Node*> NodeMap;
        NodeMap nodeMap;
        std::deque<Node*> nodesToVisit;
    
        // Set up initial new root and mapping for the root
        Node *newRoot = new Node(root->data);
        nodeMap[root] = newRoot;
    
        // Breadth-first search the graph
        nodesToVisit.push_back(root);
    
        while(!nodesToVisit.empty())
        {
            Node *cur = nodesToVisit.front();
            nodesToVisit.pop_front();
    
            Node *newCur = nodeMap[cur];
            for(int i = 0; i < 2; i++)
            {
                Node *link = cur->links[i];
                if(link)
                {
                    // If we've already created the corresponding node for this
                    // link, use that.  Otherwise, create it and add it to the map.
                    NodeMap::iterator mappedLink = nodeMap.find(link);
                    if(mappedLink != nodeMap.end())
                    {
                        newCur->links[i] = mappedLink->second;
                    }
                    else
                    {
                        Node *newLink = new Node(link->data);
                        nodeMap[link] = newLink;
                        newCur->links[i] = newLink;
                        nodesToVisit.push_back(link);
                    }
                }
            }
        }
    
        return newRoot;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked this question in an interview. If there is a pointer that
I found this interesting blog post via CodingHorror: My Favorite Interview Question . In
This is an interview Question that i was asked recently: Write a C program
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I am preparing for my interview and came across this question: Consider that i
Yesterday in my interview I was asked this question. (At that time I was
I found myself confronted with an interview question where the goal was to write
I had a recursion interview question problem in Java,Need your help on this. Write
A common interview question asks to write an algorithm that detects duplicates in a
Got asked this interesting interview question today. Explain in detail the process by which

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.