390. Design Browser History Manager
Asked in
Design Browser History Manager

Implement the GlobalBrowsingHistory class to maintain the pages visited across all normal tabs and windows of one browser profile.

Every successfully loaded page in normal browsing mode creates a separate history entry containing its globally unique visit identifier, URL, title and visit time. Visits made in private browsing mode must not be stored.

History Entry Format

Each returned history entry must use the following string format:

"visitId,url,title,visitedAt"

Class

GlobalBrowsingHistory()

Creates an empty global browsing history.

Method Signatures

Record a Visit

void recordVisit(String visitId, String url, String title, long visitedAt, boolean privateVisit)

  • visitId: A globally unique identifier supplied for the visit.
  • url: The URL of the successfully loaded page.
  • title: The title of the page.
  • visitedAt: The time at which the page was visited.
  • privateVisit: Whether the visit occurred in private browsing mode.

If privateVisit is false, save a new history entry using the supplied identifier. If it is true, do not save the entry.

Get Recent History

List<String> getRecentHistory(int limit)

  • limit: The maximum number of history entries to return.

Return at most limit entries ordered by visitedAt from greatest to smallest. If two entries have the same visit time, return the entry with the lexicographically smaller visitId first.

Search History

List<String> searchHistory(String query, long startTime, long endTime, int limit)

  • query: Text to find in the URL or title.
  • startTime: The earliest allowed visit time.
  • endTime: The latest allowed visit time.
  • limit: The maximum number of matching entries to return.

Return entries whose URL or title contains query, ignoring letter case, and whose visit time is within the inclusive range [startTime, endTime]. An empty query matches every stored entry within the requested range.

Order matching entries using the same rules as getRecentHistory.

Delete One Visit

boolean deleteVisit(String visitId)

  • visitId: The globally unique identifier of the visit to delete.

Delete the specified entry and return true. Return false if the entry does not exist.

Delete History for a URL

int deleteHistoryByUrl(String url)

  • url: The exact URL whose visits must be deleted.

Delete every entry whose URL exactly matches url and return the number of deleted entries. URL comparison is case-sensitive.

Clear History by Time

int clearHistory(long startTime, long endTime)

  • startTime: The earliest visit time to delete.
  • endTime: The latest visit time to delete.

Delete every entry whose visit time is within the inclusive range [startTime, endTime] and return the number of deleted entries.

Clear All History

int clearAllHistory()

Delete every stored history entry and return the number of deleted entries.

Rules

  • Every visitId supplied to recordVisit is globally unique.
  • A visit identifier is never reused, including after its entry is deleted.
  • Visits from all normal tabs and windows are stored together.
  • Every successful normal visit creates a separate entry.
  • Repeated visits to the same URL remain separate entries.
  • Private visits must never be stored or returned.
  • Failed or cancelled page loads are not passed to recordVisit.
  • Deleted entries must not appear in later retrieval or search results.
  • Deleting history does not close open tabs or change their current pages.

Constraints

  • At most 100,000 method calls will be made.
  • 1 ≤ visitId.length() ≤ 100
  • visitId is globally unique for every call to recordVisit.
  • 1 ≤ url.length() ≤ 2,000
  • 0 ≤ title.length() ≤ 500
  • 0 ≤ visitedAt ≤ 1,000,000,000,000,000
  • 0 ≤ startTime ≤ endTime ≤ 1,000,000,000,000,000
  • 1 ≤ limit ≤ 500
  • Visit identifiers, URLs and titles do not contain commas.

Examples

Example 1

Constructor: GlobalBrowsingHistory history = new GlobalBrowsingHistory()

Method call: history.recordVisit(visitId = "visit-1001", url = "https://docs.example.com", title = "Developer Docs", visitedAt = 100, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-1002", url = "https://news.example.com", title = "Morning News", visitedAt = 180, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-1003", url = "https://docs.example.com/guide", title = "Setup Guide", visitedAt = 140, privateVisit = false)

Output: No return value

Method call: history.getRecentHistory(limit = 2)

Output: ["visit-1002,https://news.example.com,Morning News,180", "visit-1003,https://docs.example.com/guide,Setup Guide,140"]

Example 2

Constructor: GlobalBrowsingHistory history = new GlobalBrowsingHistory()

Method call: history.recordVisit(visitId = "visit-2001", url = "https://learn.example.com/java", title = "Java Course", visitedAt = 210, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-2002", url = "https://private.example.com", title = "Private Page", visitedAt = 220, privateVisit = true)

Output: No return value

Method call: history.recordVisit(visitId = "visit-2003", url = "https://learn.example.com/python", title = "Python Course", visitedAt = 230, privateVisit = false)

Output: No return value

Method call: history.searchHistory(query = "course", startTime = 200, endTime = 240, limit = 10)

Output: ["visit-2003,https://learn.example.com/python,Python Course,230", "visit-2001,https://learn.example.com/java,Java Course,210"]

Example 3

Constructor: GlobalBrowsingHistory history = new GlobalBrowsingHistory()

Method call: history.recordVisit(visitId = "visit-3001", url = "https://shop.example.com", title = "Online Shop", visitedAt = 300, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-3002", url = "https://shop.example.com", title = "Online Shop", visitedAt = 320, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-3003", url = "https://pay.example.com", title = "Payment", visitedAt = 340, privateVisit = false)

Output: No return value

Method call: history.deleteVisit(visitId = "visit-3001")

Output: true

Method call: history.deleteHistoryByUrl(url = "https://shop.example.com")

Output: 1

Example 4

Constructor: GlobalBrowsingHistory history = new GlobalBrowsingHistory()

Method call: history.recordVisit(visitId = "visit-4001", url = "https://alpha.example.com", title = "Alpha", visitedAt = 400, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-4002", url = "https://beta.example.com", title = "Beta", visitedAt = 500, privateVisit = false)

Output: No return value

Method call: history.recordVisit(visitId = "visit-4003", url = "https://gamma.example.com", title = "Gamma", visitedAt = 600, privateVisit = false)

Output: No return value

Method call: history.clearHistory(startTime = 450, endTime = 550)

Output: 1

Method call: history.clearAllHistory()

Output: 2



Please use Laptop/Desktop or any other large screen to add/edit code.