Barclays Capital Interview Question


Country: India
Interview Type: In-Person




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

1> The issue is that you need to lock related accounts during the transaction.
2> You should avoid the global lock since it will lock all other threads.
3> With each account each lock, you need to take care of the order of lock other wise you got deadlock.
4> Use some unique id of the account, such as the account number, translate it into some unique lock order value, make sure Order(A)>Order(B), ORder(B)>ORder(C) ==> Order(A)>Order(C), and ORder(A)==Order(B)==> A==B. lock accounts in the transaction following the order of the ORder(account).

- chenlc626 March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maybe the concept of two phase commit would help here.

- liangzhou March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Account 
{
object  _lock;
double balance;

//constructor
public Account(int Id)
{
 _lock=Locker.GetLock(Id);
}

//Make this method synchronized
public Deduct(double amount)
{
lock(_lock){//deduct here}
}

//Make this method synchronized
public Add(double amount)
{
lock(_lock){//Add here}
}
}

static class Locker
{
static Dictionary<int,object> locks=new Dictionary<int,object>();

public static object GetLock(int Id)
{
if(locks.ContainsKey(id)) return locks[id]; 
else
{
Dictionary.Add(Id,(object)Id); return  locks[id];
}
}
}

- Jack March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

preserve the order locking
1 way in c++
recursive_mutex mx1 = ( from_acct.id< to_acct.id)? from.mx: to.mx;
recursive_mutex mx2 = ( from_acct.id< to_acct.id)? to.mx: from.mx;

lock_guard<recurisve_mutex> lk( mx1)
. . .

{
lock_guard<recursive_mutex> lk(mx2)
. . .


another:
std::lock( from.mx, to.mx)

- Anonymous February 20, 2019 | 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