FastPrepFind the Nth Node from the End
Problems/Find the Nth Node from the End

Find the Nth Node from the End

JP Morgan Chase logoJP Morgan Chase● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a JP Morgan Chase intern onsite interview. The author reports finding the nth node from the end of a doubly linked list of unknown length with one traversal. We chose a ListNode setup that only goes forward, the returned node value, the limits and the result for a missing node. The selection and the single-pass rule are about 90% the same as the report.

Return the value of the nth node from the end of the list starting at head. Count from 1, so n=1 selects the last node. If the list has fewer than n nodes, return -1.

The length is not supplied. Solve it in one forward pass without first counting the entire list and then starting over. Do not change the list.

The interview used a doubly linked list. This exercise exposes its forward chain through the native ListNode interface, with fields val and next (Val and Next in Go); backward links are not needed. Examples serialize the chain as a head-to-tail array, with [] for an empty list. The function returns the node value, and the missing-node result is defined above.

Function

nthFromEnd(head: ListNode, n: int) → int

Examples

Example 1

head = [10,20,30,40]n = 2return = 30
head
10203040

The last node has value 40 and the second from the end has value 30.

Example 2

head = [5]n = 2return = -1
head
5

The one-node list has no second node from the end, so return the missing-node value -1.

Constraints

  • For this exercise, assume 0 <= number of nodes <= 1000.
  • 0 <= node value <= 1000000 and 1 <= n <= 1000.
  • The input is a finite acyclic forward chain, serialized as its head-to-tail integer values.

More JP Morgan Chase Onsite Interview questions

  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
  • Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
  • K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
  • Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
  • Two SumONSITE INTERVIEW · Seen Nov 2025
  • Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
  • Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
  • Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024

Similar questions at other companies

  • Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
  • Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
  • Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
  • Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
  • Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
  • Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Reverse Linked ListThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Detect a Linked-List CycleGoogle · ONSITE INTERVIEWGoogle interview questions
  • Detect a Cycle in a Linked ListPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions

Interview reports that mention this question

  • JPMorgan Chase Intern Campus Rounds: 4 Experiences (Part 2 of 5)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public int nthFromEnd(ListNode head, int n) {
        // Write your code here.
    }
}
Checking account…