Count median in linked list

Last updated: March 7, 2026

Quick Overview

Given a singly linked list of integers, write a function to calculate and return the median value of the elements in the list. If the list has an odd number of elements, return the middle element; if it has an even number of elements, return the average of the two middle elements. The function should handle cases where the linked list is empty by returning a suitable value, such as null or zero.

Adobe
Coding & Algorithms
Software Engineer
Adobe
March 7, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

12

2,504 solved


Given a singly linked list of integers, write a function to calculate and return the median value of the elements in the list. If the list has an odd number of elements, return the middle element; if it has an even number of elements, return the average of the two middle elements. The function should handle cases where the linked list is empty by returning a suitable value, such as null or zero.

Coding interviews at Adobe focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Tree structures and recursion
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Data structure selection and trade-offs
Sorting and searching
Dynamic programming and memoization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you test this solution thoroughly?
  • Can you solve this in a single pass?
  • Can you optimize the space complexity of your solution?
  • How would you parallelize this solution?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To find the median of a singly linked list, we can utilize a two-pass approach. The first pass will help us determine the length of the list, which will then inform how we find the median in the secon...

Approach
  1. Calculate the Length: Traverse the linked list to count the total number of nodes. For example, if the linked list is 1 -> 3 -> 5, the length is 3.
  2. Determine Median Position: If the leng...

Submit Your Answer
Markdown supported

Related Questions