algorithm - Data Structure Interview -


given login/logout time of users particular website in form: (userid, login time, logout time). store data, query total number of users logged in , logged out in given time range.

what data structure should use? , how implement it?

the data structure you're looking called interval tree , has binary-search tree format start of interval (login time) values (ordering in bst).

this ds has following time complexities:

  • add interval (login-logout): o(logn)
  • remove interval: o(logn)
  • given interval [start-finish], find overlapping intervals: o(logn)