Cadence Inc Interview Question for Principal Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I'd do it,
use counting semaphores for resources
thread will acquire required resources and process for a certain amount of time
release after the sleep
Note : variable time , t and sleep(t) have been used for fine tuning the start time of threads
and would not be required if its too much trouble

public class EgThread {
    private static final Semaphore sem = new Semaphore(10);
    private static final long time = System.currentTimeMillis() + 5000; // not required
    
    public static void main(String[] args) {
        MyThread[] threads = new MyThread[5];
        
        for (int i = 0; i < 5; i++) {
            threads[i] = new MyThread(String.valueOf(i + 1));
        }
        for (int i = 0; i < 5; i++)
            threads[i].start();
        
        try {
            for (int i = 0; i < 5; i++)
                threads[i].join();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    static class MyThread extends Thread {
        String name;
        
        MyThread(String name) {
            this.name = name;
        }
        
        @Override
        public void run() {
            try {
                long t = time - System.currentTimeMillis(); // not required
                sleep(t); //---------------------------------- not required
                for (int i = 0; i < 10; i++) {
                    int req = (int) (6 * Math.random()) + 1;
                    System.out.println("T[" + name + "] run : " + (i + 1) + " asks for " + req + " resources");
                    sem.acquire(req);
                    System.out.println("T[" + name + "] run : " + (i + 1) + " acquires " + req + " resources");
                    sleep((long) (500 * Math.random()) + 200);
                    System.out.println("T[" + name + "] run : " + (i + 1) + " releases " + req + " resources");
                    sem.release(req);
                }
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

- PeyarTheriyaa November 11, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More