JP Morgan Interview Question for Applications Developers


Country: United States




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

#!/usr/bin/env python

def intersect(r1, r2):
  u_d = (r1[0] - r2[0], r1[2] + r1[0] - r2[0])
  u_c = (0, r2[2])
  v_d = (r1[1] - r2[1], r1[3] + r1[1] - r2[1])
  v_c = (0, r2[3])
  return interval_intersect(u_d, u_c) and interval_intersect(v_d, v_c)


def interval_intersect(u, v):
  return (
    (u[0] >= v[0] and u[0] <= v[1]) or
    (v[0] >= u[0] and v[0] <= u[1])
  )


def run():
  data = [
    [(1, 1, 1, 1), (-1, -1, 3, 3)],
    [(2, 2, 1, 1), (-1, -1, 3, 3)],
    [(3, 3, 1, 1), (-1, -1, 3, 3)],
    [(-1, -1, 1, 1), (-1, -1, 3, 3)],
    [(-2, -2, 1, 1), (-1, -1, 3, 3)],
    [(-3, -3, 1, 1), (-1, -1, 3, 3)],
    [(-1, 1, 1, 1), (-1, -1, 3, 3)],
    [(-2, 2, 1, 1), (-1, -1, 3, 3)],
    [(-3, 3, 1, 1), (-1, -1, 3, 3)],
    [(1, -1, 1, 1), (-1, -1, 3, 3)],
    [(2, -2, 1, 1), (-1, -1, 3, 3)],
    [(3, -3, 1, 1), (-1, -1, 3, 3)],
  ]
  for args in data:
    print "yes" if intersect(*args) else "no"


if __name__ == '__main__':
  run()

- max February 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check for every point of the smaller rectangle if it is contained within the bigger rectangle

- JimmyMVP February 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var createArray = function(X, Y, w, h) {
var retArray = [];

// width
for(var i=0; i<=w; i++) {
// height
for(var j=0; j<=h; j++) {
retArray.push( (i+X) +","+ (j+Y) );
}
}
return retArray;
};

var r1 = createArray(1,1,1,1);
var r2 = createArray(-1,-1,3,3);

var l = r1.filter(function(elem) {
return r2.indexOf( elem ) > -1;
}).length;

console.log(l);

- Anonymous February 29, 2016 | 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