Symantec Interview Question for Dev Leads


Country: India




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

#include <iostream>
#include <thread>
#include <condition_variable>
#include <mutex>

using namespace std;

int count = 0;
mutex m;
condition_variable cv;

void Run(int start, int end) {
	while (true) {
		unique_lock<mutex> lock(m);
		if (::count >= end) {
			break;
		}
		while (::count < start) {
			cv.wait(lock);
		}
		++::count;
		cout << this_thread::get_id() << " incremented to " << ::count << endl;
		cv.notify_all();
	}
}

int main()
{
	thread t1(Run, 80, 100);
	thread t2(Run, 20, 80);
	thread t3(Run, 0, 20);
	t1.join();
	t2.join();
	t3.join();
}

- Alex December 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just want to solve using high level wait() and notify()

package com.careercup.main;

public class Counter {

	public static class Thread1 extends Thread {

		private final Object lock;

		Thread1(Object lock) {
			this.lock = lock;
		}

		@Override
		public void run() {
				for (int i = 0; i < 20; i++) {
					counter++;
				}
				System.out.println("Thread1 incremented counter to :"+counter);
				synchronized (lock) {
					lock.notifyAll();
				}
		}
	}

	public static class Thread2 extends Thread {
		private final Object lock;

		Thread2(Object lock) {
			this.lock = lock;
		}
		@Override
		public void run() {
			synchronized (this) {
				try {
					System.out.println("Thread2 waiting");
					this.wait();
					for (int i = 0; i < 60; i++) {
						counter++;	
					}
					System.out.println("Thread2 incremented counter to :"+counter);
					synchronized (lock) {
						lock.notifyAll();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
	}

	public static class Thread3 extends Thread {
		@Override
		public void run() {
			synchronized(this){
				try {
					System.out.println("Thread3 waiting");
					this.wait();
					for (int i = 0; i < 20; i++) {
						counter++;
					}
					System.out.println("Thread3 incremented counter to :"+counter);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public static int counter = 0;

	public static void main(String[] args) {
		Thread thread3 = new Thread3();
		Thread thread2 = new Thread2(thread3);
		Thread thread1 = new Thread1(thread2);
		thread1.start();
		thread2.start();
		thread3.start();
		try {
			thread1.join();
			thread2.join();
			thread3.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

- sivapraneethalli December 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.example.test;

public class ThreadTest {

	public static void main(String[] args) {
		
		ThreadCounter counter = new ThreadCounter();
		Thread t1 = new Thread(counter, "T1");
		Thread t2 = new Thread(counter, "T2");
		Thread t3 = new Thread(counter, "T3");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

class ThreadCounter extends Thread{
public static int counter = 1;
	
	
	@Override
	synchronized public void run() {
		int n=20;
		if(Thread.currentThread().getName().equals("T2")){
			n = 60;
		}
		for(int i=0;i<n;i++){
			System.out.print("Running thread: "+Thread.currentThread().getName()+" ");
			System.out.println(counter);
			counter++;
		}
	}
}

- Kapil April 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.example.test;

public class ThreadTest {

public static void main(String[] args) {

ThreadCounter counter = new ThreadCounter();
Thread t1 = new Thread(counter, "T1");
Thread t2 = new Thread(counter, "T2");
Thread t3 = new Thread(counter, "T3");

t1.start();
t2.start();
t3.start();
}
}

class ThreadCounter extends Thread{
public static int counter = 1;


@Override
synchronized public void run() {
int n=20;
if(Thread.currentThread().getName().equals("T2")){
n = 60;
}
for(int i=0;i<n;i++){
System.out.print("Running thread: "+Thread.currentThread().getName()+" ");
System.out.println(counter);
counter++;
}
}
}

- Kapil April 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.List;

- Kapil April 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.example.demo;

public class ThreadTest {

	public static void main(String[] args) {

		ThreadCounter counter = new ThreadCounter();
		Thread t1 = new Thread(counter, "T1");
		Thread t2 = new Thread(counter, "T2");
		Thread t3 = new Thread(counter, "T3");

		t1.start();
		t2.start();
		t3.start();
	}
}

class ThreadCounter extends Thread {
	public static int counter = 1;
	public static String currentThread = "T1";
	int n = 20;

	@Override
	synchronized public void run() {
			while (counter < 100) {
				if (!Thread.currentThread().getName().equals(currentThread)) {
					try {
						wait();
					} catch (InterruptedException e) {
					}
				} else {
					for (int i = 0; i < n; i++) {
						System.out.print("Running thread: " + Thread.currentThread().getName() + " ");
						System.out.println(counter);
						counter++;
					}

					if (Thread.currentThread().getName().equals("T1")) {
						currentThread = "T2";
						n = 60;
					} else if (Thread.currentThread().getName().equals("T2")) {
						currentThread = "T3";
						n = 20;
					}

					notifyAll();
			}
		}
	}
}

- Sameer July 14, 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