𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠 𝐖𝐢𝐭𝐡 𝐉𝐚𝐯𝐚: 𝐃𝐢𝐠𝐞𝐬𝐭 #𝟑𝟖 This weeks collection offers insights into various aspects of Java and Spring Boot, from strategies for managing millions of requests and ensuring data consistency in microservices to enhancing application performance through database optimization. 📈 High Traffic Solutions: Scaling Spring Boot for Millions of Requests 🎲 How Java Random Seed Works 🧬 Duplicate REST API Requests: Causes and Solutions 🦣 Processing a Large Log File of ~10GB Using Java Parallel Streams 📥 Transactional Outbox Pattern: Consistency in Microservices and more.. Read the newsletter: https://lnkd.in/gD3Vtc-8 #coding #java #spring #springboot #interview
About us
Interview Preparation Resources
- Website
-
https://asyncq.com/category/dsa
External link for Interview Prep
- Industry
- Information Services
- Company size
- 1 employee
- Type
- Self-Employed
Updates
-
𝐈𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭 𝐑𝐞𝐭𝐫𝐲 𝐌𝐞𝐜𝐡𝐚𝐧𝐢𝐬𝐦 - 𝐉𝐚𝐯𝐚 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 You are designing a service that needs to communicate with an external API, which occasionally fails due to transient network issues. Describe how you would implement a retry mechanism, to handle these failures. Followup, explain when you would use a circuit breaker instead of a retry mechanism, and discuss the scenario of implementing both of them together. 𝐃𝐞𝐭𝐚𝐢𝐥𝐞𝐝 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 https://lnkd.in/gJfMN4UQ Follow Interview Prep for more. Subscribe to the Java newsletter for Java and Spring-Boot related content. https://lnkd.in/gw27QNXK #coding #java #spring #interview #programming
-
𝐑𝐞𝐩𝐥𝐚𝐜𝐞 𝐌𝐮𝐥𝐭𝐢𝐩𝐥𝐞 𝐈𝐟 𝐄𝐥𝐬𝐞 𝐂𝐨𝐧𝐝𝐢𝐭𝐢𝐨𝐧𝐬 𝐖𝐢𝐭𝐡 𝐌𝐚𝐩 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 Suppose you’re refactoring a legacy Java codebase with a long statement chain. How would you approach replacing it with a Map? What considerations would you make regarding readability and maintainability? 𝐄𝐱𝐚𝐦𝐩𝐥𝐞 𝐨𝐟 𝐥𝐞𝐠𝐚𝐜𝐲 𝐜𝐨𝐝𝐞: public void processPayment(String paymentMethod) { if ("CREDIT_CARD".equals(paymentMethod)) { processCreditCardPayment(); } else if ("PAYPAL".equals(paymentMethod)) { processPaypalPayment(); } else if ("BANK_TRANSFER".equals(paymentMethod)) { processBankTransferPayment(); } else { processDefaultPayment(); } } 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐝𝐞𝐭𝐚𝐢𝐥𝐞𝐝 𝐬𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐡𝐞𝐫𝐞: https://lnkd.in/guMp8Rnm #coding #programming #java #spring #interview
Replace Multiple If Else Conditions With Map
javabulletin.substack.com
-
𝐉𝐚𝐯𝐚 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 (𝐁𝐞𝐠𝐢𝐧𝐧𝐞𝐫): 𝐏𝐚𝐫𝐭𝐢𝐭𝐢𝐨𝐧 𝐋𝐨𝐠𝐬 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 We have been given a List of Application Logs which has properties such as id, message, tags, and datetime. We have been given the task of partitioning these logs by created datetime. 𝐄𝐱𝐚𝐦𝐩𝐥𝐞, [ApplicationLog[id=c1e1838c-1c17-4e22-a476-c604dc3dadcc, message=Logs 1, tags=[tag-1, server-4, tag-1], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=ca1f45ec-8444-4cb5-8695-08147519080f, message=Logs 2, tags=[data-1, server-1, data-2], date=Sun Sep 10 21:48:36 EDT 2023]] 𝐎𝐮𝐭𝐩𝐮𝐭 2023-09-19:[ApplicationLog[id=794fe57d-5d92-4bc3-ab62-11e9aa2d7640, message=Logs 6, tags=[server-2, server-3, data-4], date=Tue Sep 19 21:48:36 EDT 2023], ApplicationLog[id=f58b6c6f-bcd9-4cbd-9f56-e73ac951a1ac, message=Logs 21, tags=[tag-4, tag-4, tag-2], date=Tue Sep 19 21:48:36 EDT 2023], ApplicationLog[id=3f889a96-81f5-4709-a05d-cdacd4ae4afd, message=Logs 45, tags=[data-2, server-3, server-2], date=Tue Sep 19 21:48:36 EDT 2023]] 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 > For input data, we will create a Java record class as dto. > For record dto, we are constructing each property using the Random method. > Our solution streams over input data and groups records using the date field. > Since our date is in datetime format, we need to convert it yyyy-MM-dd. > For grouping records we can use groupingBy method provided by Collectors API. >Now we can print date and grouped logs as output. Follow Interview Prep for more. #coding #java #interview #dsa
-
𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐭𝐡𝐢𝐬 𝐜𝐡𝐞𝐚𝐭𝐬𝐡𝐞𝐞𝐭 𝐟𝐨𝐫 𝐲𝐨𝐮𝐫 𝐧𝐞𝐱𝐭 𝐣𝐚𝐯𝐚 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰. 𝐉𝐚𝐯𝐚 𝐒𝐭𝐫𝐞𝐚𝐦𝐬 𝐀𝐏𝐈 𝐂𝐡𝐞𝐚𝐭𝐒𝐡𝐞𝐞𝐭 The Java Streams API, introduced in Java 8, provides a powerful and flexible way to process collections of data. It allows developers to perform operations like filtering, mapping, and reducing in a functional and declarative manner, significantly simplifying the code for data processing tasks. Streams can operate in parallel to take advantage of multi-core processors, enabling more efficient computation. This cheat sheet serves as a quick reference to essential Stream methods, from creating and transforming streams to collecting results, making it easier for developers to harness the full potential of the Java Streams API. Checkout the attached cheatsheet! Follow Interview Prep for more. For more java/spring related content : https://lnkd.in/gw27QNXK #coding #programming #java #spring #interview
-
Engineering With Java: Digest #36 In this Engineering With Java newsletter edition, we have hand-picked 10 interesting Java and Spring articles worth reading. Topics include SQL, Spring AI, Decorator Pattern, Bean Validation, etc. I hope enjoy the reading! SQL Best Practices Every Java Engineer Must Know Implement Decorator Pattern For Online Payment System The Bytecode Chicken-Egg Problem Dynamic Constraint Application in Java Bean Validation Is Spring AI Strong Enough for AI? Read the full newsletter: https://lnkd.in/g436gd3M #java #coding #spring #springboot #newsletter
Engineering With Java: Digest #36
javabulletin.substack.com
-
𝐉𝐚𝐯𝐚 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 𝐎𝐟 𝐓𝐡𝐞 𝐃𝐚𝐲: 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 Suppose you’re refactoring a legacy Java codebase with a long statement chain. How would you approach replacing it with a Map? What considerations would you make regarding readability and maintainability? Example of legacy code: public void processPayment(String paymentMethod) { if ("CREDIT_CARD".equals(paymentMethod)) { processCreditCardPayment(); } else if ("PAYPAL".equals(paymentMethod)) { processPaypalPayment(); } else if ("BANK_TRANSFER".equals(paymentMethod)) { processBankTransferPayment(); } else { processDefaultPayment(); } } 𝐀𝐧𝐬𝐰𝐞𝐫: In our example, the if-else chain handles different payment methods and executes the corresponding function for each method. if the number of payment methods grows, this chain can become difficult to manage and read. Refactoring Using a Map To refactor this code, we can use a Map to store each payment method as a key and the corresponding action as the value. This approach makes the code more maintainable and easier to understand. Step-by-step refactoring: Define the Mapping: Use a Map<String, Runnable> to map each payment method to its respective processing logic. Runnable is a functional interface that allows us to execute logic without parameters. private Map<String, Runnable> paymentMethodMap = new HashMap<>(); public PaymentProcessor() { paymentMethodMap.put("CREDIT_CARD", this::processCreditCardPayment); paymentMethodMap.put("PAYPAL", this::processPaypalPayment); paymentMethodMap.put("BANK_TRANSFER", this::processBankTransferPayment); } 2. Replace the Long Statement Chain: Instead of using the if-else chain, use the map to look up the correct action based on the payment method. public void processPayment(String paymentMethod) { Runnable action = paymentMethodMap.getOrDefault(paymentMethod, this::processDefaultPayment); action.run(); } Readability Consideration Self-Descriptive Keys: We can use descriptive strings like "CREDIT_CARD", "PAYPAL" etc., to make the code more readable. We can also use constants instead of raw strings to avoid typos and provide additional clarity. 4. Maintainability Considerations Centralized Definition: The Map centralizes all payment methods and their corresponding logic, making it easy to add, modify, or remove payment methods without changing the main processing flow. paymentMethodMap.put("APPLE_PAY", this::processApplePayPayment); Avoid Duplicate Code: This approach eliminates the repetitive if-else code, making changes more straightforward and reducing the likelihood of errors. ---------------------------------------------------------------------------- Follow Interview Prep for more. Stay updated with recent java and spring boot content: https://lnkd.in/gw27QNXK #coding #java #programming #spring #springboot
-
𝐇𝐨𝐰 𝐂𝐚𝐧 𝐖𝐞 𝐈𝐧𝐣𝐞𝐜𝐭 𝐁𝐞𝐚𝐧𝐬 𝐈𝐧 𝐒𝐩𝐫𝐢𝐧𝐠? — 𝐒𝐩𝐫𝐢𝐧𝐠 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 Overview In spring, beans can be injected using various methods, including constructor injection, setter injection, and field injection. Here’s a brief overview of each method: 𝟏. 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧: Constructor injection is done by providing dependencies through a constructor. This is the preferred way of dependency injection as it makes the dependencies explicit and the object immutable. 𝟐. 𝐒𝐞𝐭𝐭𝐞𝐫 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧: Setter injection uses setter methods to inject dependencies. This method provides flexibility but makes the object mutable. 𝟑. 𝐅𝐢𝐞𝐥𝐝 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧: Field injection uses reflection to set the field directly. This method is less preferred due to the hidden nature of the dependencies and difficulties in testing. 𝐂𝐨𝐧𝐟𝐢𝐠𝐮𝐫𝐚𝐭𝐢𝐨𝐧 𝐰𝐢𝐭𝐡 @𝐂𝐨𝐧𝐟𝐢𝐠𝐮𝐫𝐚𝐭𝐢𝐨𝐧 𝐂𝐥𝐚𝐬𝐬: You can also define beans and their dependencies using a configuration class with @Bean methods. 𝐔𝐬𝐢𝐧𝐠 𝐗𝐌𝐋 𝐂𝐨𝐧𝐟𝐢𝐠𝐮𝐫𝐚𝐭𝐢𝐨𝐧: Though less common nowadays, XML configuration is another way to define beans and their dependencies. Follow up: Which method is preferred? add your answer in the comment. ------------------------------------------------------------------- Follow Interview Prep for more. Looking to stay ahead in the world of Java development? https://lnkd.in/gw27QNXK #coding #programming #interview #spring #springboot
-
𝐈𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭 𝐃𝐞𝐜𝐨𝐫𝐚𝐭𝐨𝐫 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 𝐅𝐨𝐫 𝐎𝐧𝐥𝐢𝐧𝐞 𝐏𝐚𝐲𝐦𝐞𝐧𝐭 𝐒𝐲𝐬𝐭𝐞𝐦 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧 1. Explain the Decorator Pattern. 2. Provide a Java implementation for an online payment system where different payment types (like Credit Card, PayPal, or Bank Transfer) can be customized with additional features like fraud detection, discount offers, or payment logging. 3. How does the decorator pattern help design this solution? 𝐃𝐞𝐭𝐚𝐢𝐥𝐞𝐝 𝐀𝐧𝐬𝐰𝐞𝐫 https://lnkd.in/gNd3D-a7 Follow Interview Prep for more. #coding #java #programming #interview #interviewQuestions
-
𝐔𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝𝐢𝐧𝐠 𝐋𝐞𝐞𝐭𝐜𝐨𝐝𝐞 𝐏𝐫𝐞𝐟𝐢𝐱 𝐒𝐮𝐦 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 𝐈𝐧𝐭𝐫𝐨𝐝𝐮𝐜𝐭𝐢𝐨𝐧 The Prefix Sum pattern is a common technique used in algorithms to solve problems involving subarrays, sums, and ranges more efficiently. It allows to preprocess an array and then quickly calculate the sum of elements in any subarray or find specific patterns such as balanced subarrays. This technique is particularly useful in problems where you need to calculate sums repeatedly over different ranges of an array. 𝐖𝐡𝐚𝐭 𝐢𝐬 𝐏𝐫𝐞𝐟𝐢𝐱 𝐒𝐮𝐦? A prefix sum is an array where each element at index `i` stores the cumulative sum of the elements from the start of the array up to index `i`. In other words, for an array `nums[]`, the prefix sum at index `i` is: prefix[i] = prefix[i-1] + arr[i] for i > 0 Suppose we have an array [1, 2, 3, 4, 5]. The prefix sum array would be [1, 3, 6, 10, 15]. If we need to find the sum of elements between index 1 and 3, we calculate: sum(1, 3) = prefix[3] - prefix[0] = 10 - 1 = 9. This allows for quick retrieval of subarray sums, which can help solve various problems. 𝐊𝐞𝐲 𝐔𝐬𝐞 𝐂𝐚𝐬𝐞𝐬 𝐟𝐨𝐫 𝐏𝐫𝐞𝐟𝐢𝐱 𝐒𝐮𝐦: 1. Subarray Sum Problems: — In many problems, we’re asked to find the sum of elements in a subarray, or we may be asked to check for a subarray that satisfies a particular condition (e.g., the sum equals a target). — Prefix sums help reduce the time complexity of finding the sum of any subarray from O(N) to O(1) by simply subtracting two precomputed values. 2. Subarrays with Target Sum: — In problems where we need to find subarrays whose sum equals a specific target, we can use prefix sums combined with a hash map to track previously seen sums and quickly identify whether a subarray with the desired sum exists. 𝐁𝐞𝐧𝐞𝐟𝐢𝐭𝐬 𝐨𝐟 𝐭𝐡𝐞 𝐏𝐫𝐞𝐟𝐢𝐱 𝐒𝐮𝐦 𝐏𝐚𝐭𝐭𝐞𝐫𝐧: Efficiency: It reduces the time complexity of finding subarray sums to O(1) after preprocessing. Simplicity: The pattern is easy to apply once you understand the mechanics of prefix sums and cumulative sums. 𝐂𝐨𝐧𝐜𝐥𝐮𝐬𝐢𝐨𝐧 The Prefix Sum pattern is a versatile and powerful technique for solving array-based problems involving subarray sums, range queries, and balance conditions. By preprocessing the array to compute cumulative sums and using hash maps for efficient lookups, you can significantly reduce the time complexity of many problems. Understanding and mastering this pattern is essential for efficiently solving a wide range of problems on platforms like LeetCode. 𝐁𝐨𝐧𝐮𝐬 Ready to practice the prefix sum pattern? checkout this curated page for prefix sum pattern: https://lnkd.in/g4phfbgj Follow Interview Prep for more. #coding #programming #java #leetcode