两道面试题解答之一 Trading Server Deadlocks

原创 2013年12月05日 05:55:13

https://bridgewater.interviewstreet.com/challenges/

第一题是关于判断死锁问题的

Trading Server Deadlocks

A few months ago, we were running a new release of our trade server in QA and experienced a hang. The system was accepting connections from clients, but no longer processing any transactions. This system is critical to many of our major trading operations and so, the issue was a big deal to fix before we released to production.

The trade server supports many different execution paths which take out various locks on shared resources.  Upon investigation, we found that the system had experienced a deadlock between two of those execution paths. To ensure we fixed the problem holistically (rather than just the deadlock case that we caught), we wanted to examine all the execution paths in the system and determine all cases that were constructed in a way that they can cause deadlocks.

We'll provide you a set of execution paths. Each is written as an ordered list of lock acquisitions. Your job is to write a program which enumerate all the subsets of sets of execution paths that could result in a deadlock if executed in parallel.

Your input will be a file with one execution path per line. The name will be first, separated by a space, then a comma separated list of the locks in order they are acquired. For instance:

    path1 L1,L2,L3
    path2 L2,L1,L3
    path3 L4,L5,L6
    path4 L5,L4,L6

Your output should be each subset of execution paths which can deadlock, one per line. Sort the paths alphabelitcaly and order the sets alphabetically based on their lexicographic representation.

    path1,path2
    path3,path4

For more information about deadlocks, checkout the wikipedia page.


package bridgewater;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class DeadLockSolution {
	
	private List<String> inputs = new ArrayList<String>();
	
	public DeadLockSolution(){
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = null;
		DeadLockSolution solution = new DeadLockSolution();
		try {
			InputStream inputStream = new FileInputStream("input.txt");
			Reader reader = new InputStreamReader(inputStream);
			br = new BufferedReader(reader);
			
//			br = new BufferedReader(new InputStreamReader(System.in));
			
			String input = br.readLine();
			while(input != null){
				solution.inputs.add(input);
				input = br.readLine();
			}
			solution.process(solution.inputs);
			
		} finally {
			if (br != null)
				br.close();
		}
	}

	public void process(List<String> inputs) {
		LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();
		
		for (String string : inputs) {
			String[] splits = string.split(" ");
			if(splits.length > 1){
				String[] tmp = splits[1].split(",");
				ArrayList<String> locks = new ArrayList<String>();
				locks.addAll(Arrays.asList(tmp));
				map.put(splits[0], locks);
			}
		}
//		print(map);
		
		LinkedHashSet<ArrayList<String>> ret = new LinkedHashSet<ArrayList<String>>();
		int entryIndex = 0;
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			ArrayList<String> value = entry.getValue();
			int i = 0;
			do{
				int otherEntryIndex = 0;
				for(Entry<String, ArrayList<String>> otherEntry : map.entrySet()){
					if(otherEntryIndex>entryIndex && !entry.equals(otherEntry)){
						ArrayList<String> otherValue = otherEntry.getValue();
						if(otherValue.contains(value.get(i))){
							int FirstHitIndex = otherValue.indexOf(value.get(i));
							if((i+1)%value.size() == 0){		// Special case, when looping around!
								if(FirstHitIndex+1<otherValue.size() && otherValue.get(FirstHitIndex+1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}else{
								if(FirstHitIndex-1>=0 && otherValue.get(FirstHitIndex-1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}
						}
					}
					otherEntryIndex++;
				}
				i++;
				i %= value.size();
			}while(i != 0);
			entryIndex++;
		}
		
//		System.out.println(ret);
		for (ArrayList<String> al : ret) {
			System.out.println(al.get(0)+","+al.get(1));
		}
	}
	
	public void print(Map<String, ArrayList<String>> map){
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			 String key = entry.getKey();
			 ArrayList<String> value = entry.getValue();
		    System.out.println(key + "->" + value);
		}
		
		
//		Iterator it = map.entrySet().iterator();
//	    while (it.hasNext()) {
//	        Map.Entry pairs = (Map.Entry)it.next();
//	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
//	    }
	}

}


相关文章推荐

两道面试题所引发的C指针的思考

C语言是一门使用比较广泛的高级编程语言,而指针则是C语言的精髓所在,可以说学习C语言不会灵活使用指针就谈不上精通C语言。但是由于C语言指针的灵活性导致了我们在使用过程中出现莫名其妙的各种问题,甚至是段...

两道面试题:2012年10月9号阿里巴巴笔试

1. n个无序整数,已知第i个数在排好序的序列中的位置为j,满足|i-j| 的得两分,总分是20分。 借鉴的答案: 答:由以上条件判断最小的数字一定在前k个中,于是可以讲前k个数建...

设计的两道面试题C++

C++面试题设计

两道面试题(英文版)

今天下午小陈给我发了两道面试题,我花了点时间研究了一下。 现在在这里总结: 题目一: Problem Statement # 1Given a set of N (> 1) positive i...

各位周末玩了一天累了吧,我们一起来做两道面试题吧(据说淘宝的哟)!

前言 我的CSS一直是一块短板,原因就是前几年对CSS不重视,现在重视了项目经验又不足,所以在这块上我是下了很大功夫的,也看了一点书,做了点东西,希望在几个项目后CSS水平有所提高! 完了,我最近...

两道Linux面试题

  • 2012年04月11日 22:46
  • 2KB
  • 下载

华为的两道算法笔试题及答案

  • 2011年09月01日 14:25
  • 1KB
  • 下载

sql server面试题实例

A.输出其中第3,4行: 名字倒序: select top 2 reverse(name) from t where id not in (select top 2 id from t order b...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:两道面试题解答之一 Trading Server Deadlocks
举报原因:
原因补充:

(最多只允许输入30个字)