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 6920703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:10:56+00:00 2026-05-27T10:10:56+00:00

I got an interview question on Friday and I think I flunked it. The

  • 0

I got an interview question on Friday and I think I flunked it. The question was:

Write a class that process a double linked list in PHP.

I understand the concept, and here’s the code I gave:

class element {
 private $current;
 public function __construct($e) {
  $this->current = $e;
 }
 // method
 // etc..
}

class doublelist
{
  private $prev;
  private $next;
  private $current;
  private $list;
  public function add(element $e) {
   if($this->current == NULL) {
    $this->prev = $this->current;
   }
   $this->current = $e;
  }
}

$list = new doublelist();
$list->add(new element('a'));
$list->add(new element('b'));

This works initially, but if I add a second element I “lose” the first one, and I don’t understand why.

  • 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-27T10:10:56+00:00Added an answer on May 27, 2026 at 10:10 am

    You need to be keeping track of $prev and $next on the elements, not the list. If you want to make it transparent, you can wrap each element in a bean that has pointers to the next and previous ones, or just make element have those by definition.

    The way you’re doing it right now, the list will only know which one is the current element, and which one came before that. But what you should really be doing is figuring it out from the element (or bean) which one will be the next or previous.

    Edit

    Since this question has been getting the occasional view, I thought I’d add a little code to help explain this better.

    class DoublyLinkedList {
        private $start = null;
        private $end = null;
    
        public function add(Element $element) {
            //if this is the first element we've added, we need to set the start
            //and end to this one element
            if($this->start === null) {
                $this->start = $element;
                $this->end = $element;
                return;
            }
    
            //there were elements already, so we need to point the end of our list
            //to this new element and make the new one the end
            $this->end->setNext($element);
            $element->setPrevious($this->end);
            $this->end = $element;
        }
    
        public function getStart() {
            return $this->start;
        }
    
        public function getEnd() {
            return $this->end;
        }
    }
    
    class Element {
        private $prev;
        private $next;
        private $data;
    
        public function __construct($data) {
            $this->data = $data;
        }
    
        public function setPrevious(Element $element) {
            $this->prev = $element;
        }
    
        public function setNext(Element $element) {
            $this->next = $element;
        }
    
        public function setData($data) {
            $this->data = $data;
        }
    }
    

    There are, of course, other methods you could add; and if anyone is interested in those I can add them as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got asked this interesting interview question today. Explain in detail the process by which
I got this question today in an interview: write a function to calculate the
I got this question in a Cisco interview: write a function to find the
I got this in an interview question -- the question was more about what
I just got this question on an interview and had no idea how to
Got a class that serializes into xml with XMLEncoder nicely with all the variables
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
I got following question on an interview: Which SQL mechanisms allow user to browse
I got this question in a previous interview and couldnt do it , any
I got this question in an interview with amazon. I was asked to perform

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.