- Home
- Skills
- Code Quality & Review
- Data Structures, Algorithms & Design Patterns (Java)
Works with the AI Agents you already use
Data Structures, Algorithms & Design Patterns (Java)
by Saud
Pick the right data structure, check Big O with the math, and learn the 23 GoF design patterns and secure coding.
Free
Data Structures, Algorithms & Design Patterns (Java)
Example session with this skill installed
I need an LRU cache in Java with O(1) get and put. Explain why you chose these structures.
- Read your context and instructions
- Compiled the data structures algorithms
Choice of structure
An LRU cache needs two things in O(1): find a key, and know which key was used least recently. One structure cannot do both, so I combine two:
- A hash map from key to node. It finds any key in O(1).
- A doubly linked list ordered by recency. It moves or removes a known node in O(1).
Two sentinel nodes (head and tail) remove all null checks at the ends of the list.
Code (main methods)
public V get(K key) {
Node<K, V> node = index.get(key);
if (node == null) return null;
moveToFront(node); // unlink + link at the front: O(1)
return node.value;
}
public void put(K key, V value) {
if (key == null || value == null) {
throw new IllegalArgumentException("null keys and values are not allowed");
}
Node<K, V> node = index.get(key);
if (node != null) { // existing key: update and mark as recent
node.value = value;
moveToFront(node);
return;
}
if (index.size() == capacity) { // full: evict the least recently used
Node<K, V> lru = tail.prev;
unlink(lru);
index.remove(lru.key);
}
node = new Node<>(key, value);
linkFront(node);
index.put(key, node);
}
Big O
| Operation | Time | Space |
|---|---|---|
| get | O(1) expected | O(1) |
| put | O(1) expected | O(1) |
| whole cache | - | O(capacity) |
"Expected" because the hash map is O(1) on average. The map is pre-sized so it never rehashes.
Review notes
- The cache is
not thread-safe. If several threads share it, guard both get and put with one lock (get also changes the order).
- The fixed capacity is a security feature: an unbounded cache is a memory leak and a denial-of-service risk (CWE-770).
- In production you can also use
LinkedHashMapwith access order andremoveEldestEntry.
Next steps
- Test the cache against
LinkedHashMapwith random operations. - Add a lock if the cache is shared between threads.
- Check
references/security/index.mdfor the resource-limit rules.
Connects securely to your tools. The creator never sees your data.
About this skill
Pick the right data structure, check Big O with the math, and learn the 23 GoF design patterns and secure coding. The ideas are the same in any language. All examples are written in Java, because its syntax is close to C#, C++ and many other languages. Includes working Java 17 code with 263,000+ tests, 122 security weaknesses (each with a vulnerable and a fixed example), and a code review guide.
Built while studying Data Structures at university. Every claim is backed by working code and 263,000+ automatic tests.
Requires Java 17+. Diagrams require external rendering tools rather than ASCII. Scope limited to 122 specific security weaknesses and GoF patterns.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 12 days ago
- Free with an Agensi account
- Software
- Java 17+ (to run the sample code)
Permissions
What's inside
Frequently Asked Questions
New in Code Quality & Review
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Code Quality & Review.