Handling Pagination in Java: What the Interviewers Actually Want
Pagination is one of those topics that shows up in coding interviews more often than you'd expect, mostly because it touches on array manipulation, boundary conditions, and basic performance awareness. The HackerRank version usually asks you to implement a method that splits a list into pages of a given size and returns the items on a specific page number. I spent a week debugging my first attempt at this during an online assessment. The issue was subtle but classic: I was using 1-based page numbering in my head but 0-based indexing in my code, which meant page 1 returned empty results whenever the list size was a multiple of the page size. The fix was straightforward once I wrote out the formula on paper.
Website Pagination Hackerrank Solution Java
Here is the approach that actually works. The core logic breaks down into three steps: calculate the starting index, calculate the ending index, then extract the sublist. Most candidates write these as separate variables. I prefer to keep it to two lines to reduce the chance of off-by-one errors sneaking in. The pageNumber is 1-based, which is the convention most interviewers expect. That is why the formula subtracts 1 from pageNumber before multiplying by pageSize. If you use 0-based page numbers here, you will fail the hidden test cases even if your logic looks correct. There is a boundary condition that trips people up. When the requested page exceeds the total number of pages available, the method should return an empty list rather than throwing an exception. The check if (start >= items.size()) handles this in one line. I used to write a separate calculation for total pages and then compare page numbers. That was more code and equally error-prone.
Another thing worth noting: the sublist method returns a view, not a copy. If the caller modifies the returned list and then tries to use the original, you get unexpected behavior. Wrapping it in a new ArrayList prevents that. This detail rarely comes up in the problem statement but showing you know it separates good candidates from average ones. The time complexity is O(k) where k is the page size, because sublist operations are constant time and the new ArrayList constructor copies only the requested elements. Space complexity is also O(k) for the same reason. This is efficient enough for interview purposes, though production systems usually handle pagination at the database level to avoid loading entire datasets into memory. If you are working with very large datasets in a real application, consider using stream slicing or database-level LIMIT and OFFSET instead. The HackerRank problem assumes everything fits in memory, so the ArrayList approach is the expected answer. But knowing when to push pagination down to the query layer is what makes you a stronger engineer.
Get the Full Details

The edge case of zero or negative page numbers should return empty lists or throw IllegalArgumentException depending on the problem constraints. Read the specification carefully. Some versions expect a runtime exception for invalid input, others expect graceful handling. Getting this wrong costs points even when the core algorithm is correct.