String Interning
Interning can reduce memory usage when the same strings occur repeatedly.
It can make identity comparisons meaningful for interned references, although content comparison should normally use equals().
Interning has overhead, so it should not be applied indiscriminately to high-cardinality data.
Immutable strings are important because shared instances must not be modifiable.
Suppose you need to compare two strings for equality in Java. How would using String.intern() affect the comparison, and what would you write?
If you have a loop that creates many identical string literals, what happens if you call intern() on each one? Explain the memory impact.
Your team added a feature that stores user‑entered tags in a global pool using String.intern(). After deployment you notice increased GC pauses. What could be causing this, and how would you investigate?
During a code review you see a method that calls intern() on every incoming request parameter. What trade‑offs would you discuss with the author?
Design a service that processes millions of log lines per second and needs to deduplicate repeated messages. Would you rely on Java's built‑in string interning, or implement a custom string pool? Explain the performance and memory considerations.
Your application runs on a constrained JVM with limited heap. How would you decide the size of the string intern pool, and what safeguards would you put in place to avoid OOM?
A legacy monolith uses String.intern() extensively, and you are planning a migration to microservices. What architectural concerns arise from continuing to rely on the JVM intern pool across services, and how would you redesign the system to maintain deduplication without cross‑process interning?
Your organization wants to standardize string handling across multiple languages (Java, C#, Go). How would you approach providing a consistent interning strategy, and what impact would it have on cross‑team contracts and observability?