Design Browser History

package org.wesome.dsalgo;/*
 * You have a browser with one tab where you start on the homepage and you can visit another URL,
 * get back in the history number of steps or move forward in the history number of steps.
 */

import java.util.ArrayList;
import java.util.List;

class BrowserHistory {

    List<String> history;
    int currentIndx;

    public BrowserHistory(String homepage) {
        history = new ArrayList<>();
        history.add(homepage);
        currentIndx = 0;
    }

    /*Visits URL from the current page. It clears up all the forward history.*/
    public String visit(String url) {
        if (currentIndx != history.size() - 1) {
            /*  if current Indx is not equal to history size, means either back has been called before or we are revisiting the page, hence clear up all the forward history  */
            for (int indx = history.size() - 1; indx > currentIndx; indx--)
                history.remove(indx);
        }
        history.add(url);
        currentIndx++;
        return url;
    }

    /*Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current URL after moving back in history at most steps.*/
    public String back(int backSteps) {
        /*  if backSteps is greater than total history steps    */
        if (currentIndx - backSteps < 0) {
            currentIndx = 0;
        } else {
            /*  step move backwards in history */
            currentIndx = currentIndx - backSteps;
        }
        return history.get(currentIndx);
    }

    /*Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current URL after forwarding in history at most steps.*/
    public String forward(int forwardSteps) {
        /*  if forwardSteps is greater than total history steps    */
        if (forwardSteps + currentIndx >= history.size()) {
            currentIndx = history.size() - 1;
        } else {
            currentIndx = currentIndx + forwardSteps;
        }
        return history.get(currentIndx);
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class BrowserHistoryTest {
    @Test
    public void test() {
        BrowserHistory browserHistory = new BrowserHistory("wesome.org");
        Assertions.assertEquals("google.com", browserHistory.visit("google.com")); // You are in "0.com". Visit "google.com"
        Assertions.assertEquals("yahoo.com", browserHistory.visit("yahoo.com")); // You are in "google.com". Visit "yahoo.com"
        Assertions.assertEquals("youtube.com", browserHistory.visit("youtube.com")); // You are in "yahoo.com". Visit "youtube.com"
        Assertions.assertEquals("yahoo.com", browserHistory.back(1)); // "yahoo.com"
        Assertions.assertEquals("google.com", browserHistory.back(1)); // "google.com"
        Assertions.assertEquals("yahoo.com", browserHistory.forward(1)); // "yahoo.com"
        Assertions.assertEquals("facebook.com", browserHistory.visit("facebook.com")); // You are in "yahoo.com". Visit "facebook.com"
        Assertions.assertEquals("facebook.com", browserHistory.forward(2)); // "facebook.com", cannot move forward.
        Assertions.assertEquals("google.com", browserHistory.back(2)); // "google.com"
        Assertions.assertEquals("wesome.org", browserHistory.back(7)); // "wesome.org"
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on