FastPrepAddress on Same Page

Address on Same Page

NVIDIA logoNVIDIA● EasyINTERNOA

Problem statement

You are given two unsigned 32-bit memory addresses, addr1 and addr2.

Memory is split into aligned 4 KB pages. Each page contains 4096 consecutive byte addresses: the first page covers addresses 0 through 4095, the next covers 4096 through 8191, and so on.

Return 1 if both addresses belong to the same page. Otherwise, return 0.

Two addresses share a page when their highest 20 bits match. Their lowest 12 bits give the offset within the page and may differ.

Function

samePage(addr1: long, addr2: long) → int

Examples

Example 1

addr1 = 0addr2 = 4095return = 1

Addresses 0 and 4095 both lie in the first page, which covers addresses 0 through 4095.

Example 2

addr1 = 4095addr2 = 4096return = 0

Address 4095 is in the page from 0 through 4095. Address 4096 starts the next page, so the addresses are on different pages.

Example 3

addr1 = 65536addr2 = 69631return = 1

Both addresses lie in the page from 65536 through 69631. Their offsets differ, but their page number is 16.

Constraints

  • 0 <= addr1 <= 2^32 - 1
  • 0 <= addr2 <= 2^32 - 1

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int samePage(long addr1, long addr2) {
    // Write your code here.
}
addr10
addr24095
expected1
Checking account…