Intuit Interview Question for Senior Software Development Engineers


Country: United States




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

public class ProduceConsume1 {
	static int countProduce = 0;
	// ----------------------
	public static class Producer implements Runnable {
		private final BlockingQueue<URI> queue;
		private boolean run = true;

		public Producer(BlockingQueue<URI> q) {
			queue = q;
		}

		public void run() {
			try {
				while (run) {
					queue.put(produce());
					if (countProduce % 10 != 0) {
						System.out.println("sleep 0.1c for fast requests "
								+ countProduce);
						Thread.sleep(100);
					} else {
						System.out.println("sleep 2c for slow requests "
								+ countProduce);
						Thread.sleep(2000);
					}
					countProduce++;
					Thread.sleep(10);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public URI produce() throws URISyntaxException {
			URI url = new URI("someurl/webhp?#q=" + countProduce);
			System.out.println("generate url " + url);
			return url;
		}
	}
	// ---------------------
	public static class Consumer implements Runnable {
		private final BlockingQueue<URI> queue;
		private boolean run = true;
		private ExecutorService executor;
		private List<FutureTask<String>> futures;
		private List<URI> urls;

		public Consumer(BlockingQueue<URI> q) {
			queue = q;
			futures = new ArrayList<FutureTask<String>>();
			ThreadFactory threadFactory = Executors.defaultThreadFactory();
			executor = Executors.newFixedThreadPool(3, threadFactory);
			urls = new ArrayList<URI>();
		}

		public void run() {
			try {
				while (run) {
					urls.clear();
					queue.drainTo(urls);
					if (!urls.isEmpty()) {
						final URI lastURI = urls.get(urls.size() - 1);

						// cancel all
						if (!futures.isEmpty()) {
							for (int i = 0; i < futures.size(); i++) {
								System.out.println("cancel request "
										+ futures.get(i).toString());
								if (!futures.get(i).isCancelled()) {
									futures.get(i).cancel(true);
								}
							}
							futures.clear();
						}

						// create new task for rq
						FutureTask<String> future = new FutureTask<String>(
								new Callable<String>() {
									public String call()
											throws URISyntaxException {
										System.out.println("call request "
												+ lastURI);
										// call rq and wait for answer
										try {
											System.out
													.println("wait for answer 0.5sec   "
															+ lastURI);
											Thread.sleep(500);
											System.out
													.println("+++ got response from "
															+ lastURI);
											return "response from  " + lastURI;
										} catch (Exception e) {
											System.out
													.println("--- canceled / interrupted request "
															+ lastURI);
											return "canceled / interrupted request "
													+ lastURI;
										}
									}

									@Override
									public String toString() {
										return lastURI.toString();
									}
								});

						futures.add(future);
						executor.submit(future);
					}
					Thread.sleep(10);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			executor.shutdown();
		}
	}
	// ----------------------
	public static void main(String[] args) {
		BlockingQueue<URI> q = new LinkedBlockingQueue<URI>();
		Producer pr = new Producer(q);
		Consumer c = new Consumer(q);
		Thread prt = new Thread(pr);
		Thread ct = new Thread(c);

		prt.start();
		ct.start();

		try {
			Thread.sleep(10000);
			pr.run = false;
			c.run = false;
			prt.join();
			ct.join();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

- Oleg G September 24, 2014 | 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