mission-peace/interview · error · IllegalArgumentException
Duplicate date
Error message
Duplicate date
What it means
RedBlackTree.insert does not allow duplicate keys; when the descent reaches a node whose data equals the value being inserted it throws IllegalArgumentException("Duplicate date " + data). This is a deliberate invariant of this tree implementation rather than an accidental bug (note the 'date' typo in the message).
Solutions
- Check search(data) != null before calling insert and skip/update instead of re-inserting
- Deduplicate the input collection before bulk loading into the tree
- Catch IllegalArgumentException around insert if duplicates should be silently ignored
Example fix
// before
tree.insert(date);
// after
if (tree.search(date) == null) {
tree.insert(date);
} Defensive patterns
Strategy: try-catch
Validate before calling
if (tree.search(data) != null) { /* already present, skip */ } else { tree.insert(data); } Try / catch
try {
tree.insert(data);
} catch (IllegalArgumentException e) {
// duplicate key, ignore or update
} Prevention
- Deduplicate input collections before tree insertion
- Check membership with search() before inserting
- Remember duplicates are unsupported in this RedBlackTree; use a multimap variant if needed
When it happens
Trigger: Calling insert(data) with a value already present in the tree, including the initial insert(root, data) call and recursive insert(root, node, data) descents.
Common situations: Loading a dataset with repeated keys (e.g. duplicate dates in a time-series index), re-inserting an already processed record, or retry logic that replays an insert without checking membership.
Related errors
AI-assisted analysis of mission-peace/interview@94be5deb0c (2026-09-08).
Data as JSON: /api/errors/c36d187b08019a2d.
Report an issue: GitHub.
Appendix: source
Thrown at src/com/interview/tree/RedBlackTree.java:190
} else {
return false;
}
}
private Node insert(Node parent, Node root, int data) {
if(root == null || root.isNullLeaf) {
//if parent is not null means tree is not empty
//so create a red leaf node
if(parent != null) {
return createRedNode(parent, data);
} else { //otherwise create a black root node if tree is empty
return createBlackNode(data);
}
}
//duplicate insertion is not allowed for this tree.
if(root.data == data) {
throw new IllegalArgumentException("Duplicate date " + data);
}
//if we go on left side then isLeft will be true
//if we go on right side then isLeft will be false.
boolean isLeft;
if(root.data > data) {
Node left = insert(root, root.left, data);
//if left becomes root parent means rotation
//happened at lower level. So just return left
//so that nodes at upper level can set their
//child correctly
if(left == root.parent) {
return left;
}
//set the left child returned to be left of root node
root.left = left;
//set isLeft to be true
isLeft = true;
} else {View on GitHub (pinned to 94be5deb0c)