FastPrepIterate Canonical IPv6 Addresses

Iterate Canonical IPv6 Addresses

OpenAI logoOpenAI● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

start is an IPv6 address in canonical lowercase form: exactly eight groups of four hexadecimal digits separated by colons. Apply every value in moves cumulatively. A move of 1 increments the unsigned 128-bit address, and -1 decrements it.

Return the canonical address after each move. The input guarantees that no step underflows below zero or overflows above 2^128 - 1.

Function

moveIPv6Addresses(start: String, moves: int[]) → String[]

Examples

Example 1

start = "0000:0000:0000:0000:0000:0000:0000:00ff"moves = [1,1,-1]return = ["0000:0000:0000:0000:0000:0000:0000:0100","0000:0000:0000:0000:0000:0000:0000:0101","0000:0000:0000:0000:0000:0000:0000:0100"]

Carry crosses the low byte boundary and moves are cumulative.

Example 2

start = "0000:0000:0000:0000:ffff:ffff:ffff:ffff"moves = [1,-1]return = ["0000:0000:0000:0001:0000:0000:0000:0000","0000:0000:0000:0000:ffff:ffff:ffff:ffff"]

Carry and borrow cross the 64-bit midpoint.

Example 3

start = "abcd:0000:0000:0000:0000:0000:0000:0000"moves = []return = []

No moves produce no addresses.

Constraints

  • 0 <= moves.length <= 200000.
  • Every move is exactly -1 or 1.
  • start has eight four-digit lowercase hexadecimal groups.
  • Every intermediate address stays in the unsigned 128-bit range.

More OpenAI problems

See OpenAI hiring insights
public String[] moveIPv6Addresses(String start, int[] moves) {
    // Write your solution here.
}
start"0000:0000:0000:0000:0000:0000:0000:00ff"
moves[1,1,-1]
expected["0000:0000:0000:0000:0000:0000:0000:0100", "0000:0000:0000:0000:0000:0000:0000:0101", "0000:0000:0000:0000:0000:0000:0000:0100"]
Checking account…