Magicsheet logo

Defanging an IP Address

Easy
53.5%
Updated 6/1/2025

Defanging an IP Address

What is this problem about?

The Defanging an IP Address interview question is a simple string manipulation task. A defanged IP address replaces every period "." with "[.]". This is often used in security contexts to prevent links from being clickable or automatically parsed as network addresses. This Defanging an IP Address coding problem is a basic check of your string replacement skills.

Why is this asked in interviews?

Companies like Meta and Bloomberg use this as a very basic "warm-up" or "ice-breaker" question. It tests if a candidate knows the basic string API of their language or if they can manually traverse and reconstruct a string. It’s a test of fundamental coding accuracy.

Algorithmic pattern used

This follows the String interview pattern.

  1. Built-in approach: Use a function like .replace(".", "[.]").
  2. Manual approach: Iterate through the string character by character. If the character is a period, append "[.]" to a result builder; otherwise, append the character itself.

Example explanation

Input: 1.1.1.1

  1. '1' -> "1"
  2. '.' -> "1[.]"
  3. '1' -> "1[.]1" ... and so on. Result: 1[.]1[.]1[.]1

Common mistakes candidates make

  • Regex errors: In some languages, replace takes a regex. Since . is a wildcard in regex, you must escape it: replace(/./g, "[.]"). Forgetting the escape will replace every character in the string.
  • String Immutability: In languages like Java or Python, strings are immutable. Repeatedly concatenating strings in a loop is O(N^2). Using a StringBuilder or list join is O(N).

Interview preparation tip

Always be aware of how your language's replace function works. Does it replace all occurrences or just the first? Does it use regex? Knowing these details shows you understand your tools.

Similar Questions