Find the Nth Node from the End
Updated
Problem statement
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) → intExamples
Example 1
head = [10,20,30,40]n = 2return = 30The last node has value 40 and the second from the end has value 30.
Example 2
head = [5]n = 2return = -1The 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 <= 1000000and1 <= 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
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)