Scrivio Interview library

Arrays

28 questions in 8 subtopics.

Array creation and default values

CodingFoundations7 min read
What do these print?
int[] a = new int[3];
String[] s = new String[2];
boolean[] f = new boolean[1];
System.out.println(a[0] + " " + s[0] + " " + f[0] + " " + a.length);
int[] b = {};
System.out.println(b.length);
ConceptIntermediate7 min read
Should a method that finds no results return null or an empty array or collection? What does the empty-array choice cost and how do you avoid it?
InternalsIntermediate7 min read
How is an array laid out in HotSpot memory, and why is iterating an int[] typically much faster than iterating an Integer[] of the same values?

Arrays utilities

ConceptFoundations7 min read
Which sorting algorithms does Arrays.sort use for primitive arrays and for object arrays, and why are they different?
CodingFoundations7 min read
Why does this print false and what should you call instead?
int[] x = {1, 2};
int[] y = {1, 2};
System.out.println(x.equals(y) + " " + x);
CodingIntermediate7 min read
Arrays.binarySearch returns a negative number when a key is missing. How do you use that value to insert the key into a sorted array, and what goes wrong if the array is not sorted?
DebuggingIntermediate7 min read
Code calls List<Integer> list = Arrays.asList(1, 2, 3); list.add(4); and fails, while another place modifies the original array and sees the list change. Explain both.
LeadershipPrincipal7 min read
Your organization's high-throughput services still pass large primitive arrays between components for performance, while most teams prefer collections. Looking ahead to the Vector API, value classes and the Foreign Function and Memory API, how do you decide what to standardise?

Multi-dimensional arrays

CodingFoundations7 min read
How is int[][] grid = new int[3][]; different from new int[3][4]? Write code that creates a triangular array where row i has i + 1 elements.
ScenarioIntermediate8 min read
An image processing service stores pixels as int[height][width] and is slow when iterating column by column. What would you change?
LeadershipPrincipal8 min read
Teams doing numeric and data-heavy work in Java ask whether to standardise on arrays, a third-party matrix library, or wait for platform features such as value classes and the Vector API. How do you make this multi-year bet?

Array copying

ConceptFoundations7 min read
Compare System.arraycopy, Arrays.copyOf, Arrays.copyOfRange and clone() for copying arrays. Are the copies deep or shallow?
DebuggingIntermediate7 min read
A class returns its internal array from a getter and stores the array passed to its constructor. A caller's later changes corrupt the object's state. What review comments do you make?
DesignAdvanced7 min read
A shared library returns internal arrays from many public methods, and several services have corrupted its state by modifying them. Changing the return types to List would break binary compatibility. How do you fix this over several releases?

Varargs

CodingFoundations6 min read
What happens with each call?
static void log(String... args) {
System.out.println(args == null ? "null" : args.length);
}
log();
log("a", "b");
log((String) null);
log((String[]) null);
DesignIntermediate7 min read
When designing a public API, when is varargs a good choice, and what pitfalls do overloads with varargs create? Mention why List.of has overloads with fixed arity.
ScenarioAdvanced8 min read
A widely used internal logging API has a method log(String format, Object... args). Profiling across the fleet shows it allocates an array on every call even when the log level is disabled. How do you change the API without breaking callers?

Array covariance and ArrayStoreException

DebuggingFoundations5 min read
Why does this compile and then fail at run time?
Object[] objs = new String[2];
objs[0] = Integer.valueOf(1);
ConceptAdvanced7 min read
Array covariance was a Java 1.0 decision, while generics added in Java 5 are invariant. Explain the consequences of this mismatch for library design today.
ConceptPrincipal6 min read
Array covariance is now widely considered a mistake in Java's design, yet it cannot be removed. How should a large organization deal with language features that are known to be flawed but permanent?

Common array operations

CodingFoundations6 min read
Remove duplicates from a sorted int[] in place and return the new logical length, for example {1,1,2,3,3,3,4} returns 4 with the first four elements {1,2,3,4}. Then explain how you would do it for an unsorted array.
CodingFoundations6 min read
Write a method that rotates an int[] to the right by k positions in place with O(1) extra space, for example {1,2,3,4,5} with k = 2 becomes {4,5,1,2,3}. Handle k larger than the length.
InternalsIntermediate7 min read
Compare the cost of filling, searching and summing a large int[] with a for loop, Arrays.fill, Arrays.stream(a).sum() and IntStream.parallel(). When does each make sense in a service?
DesignAdvanced7 min read
A pricing engine keeps hot data in large primitive arrays for speed, and new team members keep introducing bugs with index arithmetic. How do you keep the performance while making the code safer?

Array conversions

ConceptFoundations7 min read
How do you convert between byte[] and String, and between char[] and String, correctly? Why are passwords often kept in char[] rather than String?
CodingFoundations6 min read
Convert between these forms and point out the traps: int[] to List<Integer>, List<Integer> to int[], String[] to List<String> that can be resized, and List<String> to String[].
DebuggingIntermediate6 min read
Code does
Object[] raw = list.toArray();
String[] names = (String[]) raw;
and fails with ClassCastException at runtime although every element is a String. Explain why, and also why a generic method
<T> T[] toArray(List<T> l) {
return (T[]) l.toArray();
}
is dangerous.
LeadershipPrincipal7 min read
Your services exchange large numeric datasets and repeatedly convert between primitive arrays, boxed collections, byte buffers and JSON, which shows up as a major CPU and GC cost. What long-term direction do you set?