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); } } ...

Number of cells in multiplication table containing X

Problem Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table. Suppose you have a multiplication table that is N by N. That is, a 2D array where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 0-indexed) or i * j (if 1-indexed). ...

Largest Binary Search Tree BST Subtree

Problem Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note: A subtree must include all of its descendants. Examples Example 1: 10 / \ [5] 15 / \ \ [1] [8] 7 ...

Binary Tree Inorder Traversal

Problem Given a binary tree, write a non recursive or iterative algorithm for Inorder traversal. Inorder Traversal First, visit all the nodes in the left subtree Then the root node Visit all the nodes in the right subtree inorder(root->left) display(root->data) inorder(root->right) Example Example 1: 1 \ 2 / 3 ...

Implement Queue using Stacks

Problem Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false otherwise. Notes: ...

Find fixed point in sorted array

Problem Given a sorted array of distinct integers, write a function to find the magic index or fixed point in the array, else return -1. Definition Magic index or a Fixed point in an array is an index i in the array A such that A[i] = i. Examples Example 1: Input: nums = [-6, 0, 2, 40] Output: 2 ...

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