Alternating Two-Instance Singleton Pattern Problem

Problem Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of getInstance(), return the first instance and in every odd call of getInstance(), return the second instance. Solution Method 1 - Using Singleton Pattern Here is the explanation: instances: An array holding the two singleton instances. callCount: A counter to keep track of how many times getInstance is called. Private Constructor: Ensures that the class cannot be instantiated directly. synchronized getInstance: Synchronisation is used to make the method thread-safe. It ensures that only one thread can execute this method at a time, avoiding the creation of multiple instances in a multi-threaded environment. Modulo Operator (%): Used to alternate between the two singleton instances based on the call count. Lazy Initialization: The instances are created only when they are first needed. Code Java public class TwoInstanceSingleton { private static final TwoInstanceSingleton[] instances = new TwoInstanceSingleton[2]; private static int callCount = 0; // Private constructor to prevent instantiation private TwoInstanceSingleton() {} public static synchronized TwoInstanceSingleton getInstance() { int index = callCount % 2; // Lazily initialize the instance if (instances[index] == null) { instances[index] = new TwoInstanceSingleton(); } callCount++; return instances[index]; } // Example usage public static void main(String[] args) { TwoInstanceSingleton obj1 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj2 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj3 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj4 = TwoInstanceSingleton.getInstance(); System.out.println(obj1 == obj2); // false System.out.println(obj1 == obj3); // true System.out.println(obj2 == obj4); // true System.out.println(obj1 == obj4); // false } } ...

Capturing Loop Variables in Lambdas Problem

Problem What does the below code snippet print out? How can we fix the anonymous functions to behave as we’d expect? functions = [] for i in range(10): functions.append(lambda : i) for f in functions: print(f()) Solution Method 1 - Using default argument at creation The given code snippet will print the number 9 ten times. This happens because the anonymous functions (lambdas) capture the variable i by reference. When the functions are eventually called in the print loop, they all refer to the final value that i held in the for loop, which is 9. ...

Implement Job Scheduler with Delay Function Execution

Problem Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds. Solution Implementing a job scheduler that invokes a function f after n milliseconds can be achieved using threading and timers Code Java import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class JobScheduler { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void jobScheduler(Runnable f, int n) { scheduler.schedule(f, n, TimeUnit.MILLISECONDS); } public static void main(String[] args) { JobScheduler js = new JobScheduler(); Runnable exampleFunction = new Runnable() { @Override public void run() { System.out.println("Function called!"); } }; // Call exampleFunction after 2000 milliseconds (2 seconds) js.jobScheduler(exampleFunction, 2000); } } ...

Implementing car and cdr Functions for Pair Construction

Problem cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. Given this implementation of cons: def cons(a, b): def pair(f): return f(a, b) return pair ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy