Implement the simulateSimpleBrowserBack method that simulates browser back navigation and returns the final page.

The input contains an array pages, its length size, and an integer backs. The array represents browser history in the order the pages were visited.

Your task is to return the page that will be open after pressing the browser back button backs times from the current page. The current page is the last page in the array.

For example, if the history is [home,about,contact] and backs is 1, the browser moves from contact to about.

Example 1

Input
pages (string[]) = [home,about,contact]
size (int) = 3
backs (int) = 1
Returns (string) about

Example 2

Input
pages (string[]) = [home,about,contact]
size (int) = 3
backs (int) = 2
Returns (string) home

Example 3

Input
pages (string[]) = [a,b,c,d]
size (int) = 4
backs (int) = 0
Returns (string) d

The current page is located at index size - 1. Each back operation moves one position to the left in the history.

So, subtract backs from the last index to find the final page index. If the number of back operations is greater than the available history, stay at the first page.

After calculating the safe index, return the page stored at that position.

Pseudocode:

function simulateSimpleBrowserBack(pages, size, backs):
    if size == 0:
        return empty string
    index = size - 1 - backs
    if index < 0:
        index = 0
    return pages[index]
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.