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 ...

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

Nth Number with sum of digits as 10

Problem A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. Examples Example 1: Input: n = 1 Output: 19 Example 2: Input: n = 2 Output: 28 Solution Method 1 - Brute Force Keep on generating the numbers with sum of digits as 10, and when count is n, return the number. ...

Buildings with sunset view Problem

Problem You are given an array representing the heights of neighboring buildings on a city street, from east to west. The city assessor would like you to write an algorithm that returns how many of these buildings have a view of the setting sun, in order to properly value the street. Assumption: Good to clarify with the interviewer, if the heights are equal? For this problem, equal height building don’t obstruct the view, and should be included in answer. ...

Minimum Level Sum of a Binary Tree Problem

Problem Given a binary tree, return the level of the tree with minimum sum. OR Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is minimal. Opposite of this problem - Minimum Level Sum of a Binary Tree Problem. ...

Construct Binary Tree from Inorder and Preorder Traversal

Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Count Inversions - Count Smaller on Right

Problem Given an integer array, count the number of inversions. What is an inversion? This was taught in Algorithms: Design and Analysis Part I on coursera - how to count inversions. Inversion count is the distance of order of elements in an array from the the sorted order i.e. it indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum. ...

Find Median from Data Stream

Problem Given that integers are read from a data stream. Find median of elements read so for in efficient way. OR Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. OR You are given a stream of numbers which can be positive or negative. You are required to provide an operation FIND MEDIAN..which when invoked should be able return the median of the numbers in stream(in say O(1) time) ...

Implement rand7() using rand5()

Problem Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. (i.e., implement rand7() using rand5()). OR Using a function rand5() that returns an integer from 1 to 5 (inclusive) with uniform probability, implement a function rand7() that returns an integer from 1 to 7 (inclusive). Solution This appear to be one of those probabilistic analysis questions. You should be familiar with the concept of expected value, as it could be extremely helpful in probabilistic analysis. ...

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