• retagged by
257 views
9 9 votes

Consider the problem of finding the middle node in a list $l$ of size $n$, given that $n$ is odd. Count the number of accesses to positions of list $l$ needed to find the middle node using the most time-efficient algorithm, and only $\text{O(1)}$ space. If the same position is accessed several times, count each of those accesses separately. Let $x/y$ denote integer division. How many accesses to positions of list $l$ does the most efficient algorithm perform to find the middle node when $l$ is implemented by a singly-linked list without a size field?

  1. $1$
     
  2. $n + 1$
     
  3. $(n + 2) / 2$
     
  4. $(3n + 2) / 2$

2 Answers

3 3 votes

We'll be using the using the optimal Two-Pointer (Tortoise and Hare Algorithm)

 

Let's define an "access" as a pointer moving to or landing on a node.

  1. Initialization: Both the $\texttt{slow}$ and $\texttt{fast}$ pointers start at the head of the list $($Node $1)$.

    • Accesses $= 2$

  2. The Loop: In each step of the traversal:

    • The $\texttt{fast}$ pointer moves forward $2$ nodes (e.g., from Node $1 \rightarrow$ Node $2 \rightarrow$ Node $3)$. This requires reading the links sequentially. $(2$ accesses$)$

    • The $\texttt{slow}$ pointer moves forward $1$ node $($e.g., from Node $1 \rightarrow$ Node $2)$. $(1$ access$)$

    • Total accesses per iteration $=3$

  3. Number of Iterations: To reach the end of an $n$-length list, the fast pointer takes $n−1$ steps. Since it takes $2$ steps per iteration, there are exactly $\dfrac{n−1​}{2}$​ iterations.

 

Now, 

Total Accesses $= ($Initial accesses$) + ($Iterations $\times$ Accesses per iteration$)$

Total Accesses$=2+\dfrac{(n−1​)}{2}\times3$

Total Accesses$=\dfrac{4+3n−3​}{2}=\dfrac{3n+1​}{2}$

Now, because the problem specifies integer division, for any odd $n$, 

the expression $\dfrac{3n+1​}{2}$​ yields the exact same integer result as $\dfrac{3n+2}{2}$​.

 

Answer : D

• edited by
2 2 votes


Code for Slow Pointer / Fast Pointer approach for optimal position accesses.

 

MiddleNode(head):
    slow = head
    fast = head

    while true:
        A = fast.next
        if A == NULL:
            return slow

        B = A.next
        if B == NULL:
            return slow

        fast = B
        slow = slow.next

 

Answer: D

Answer:
Position:
Show:

Related questions

7 7 votes
4 4 answers
255
255 views
GO Classes asked Jul 4
255 views
Consider the following C function intended to delete the last node of a singly linked list:void removeLast(struct Node *head) { struct Node *p = head; struct Node *q = p-...
3 3 votes
3 3 answers
238
238 views
GO Classes asked Jul 4
238 views
Consider the following singly linked list:$\texttt{'B' - 'A' - 'S' - 'E' - NULL}$The pointer $\texttt{head}$ points to the first node containing $\texttt{'B'}$.What chara...
6 6 votes
2 2 answers
246
246 views
GO Classes asked Jul 4
246 views
A singly linked list maintains two pointers:struct Node *front; // points to first node struct Node *rear; // points to last nodeWhich operation would be inefficient when...
6 6 votes
3 3 answers
246
246 views
GO Classes asked Jul 4
246 views
Consider the following structure:struct Node { int data; struct Node *next; };A function should insert a new node containing value $\texttt{x}$ at the beginning of a sing...