Given entrance and exit logs, find the population maximum I was given this problem during an interview. And I solved it using Python. Would like to get feedback to see how I can improve my interview response. 


  Busiest Time in The Mall
  
  The Westfield Mall management is trying to figure out what the busiest
  moment at the mall was last year. You’re given data extracted from the
  mall’s door detectors. Each data point is represented as an integer
  array whose size is 3. The values at indices 0, 1 and 2 are the
  timestamp, the count of visitors, and whether the visitors entered or
  exited the mall (0 for exit and 1 for entrance), respectively. Here’s
  an example of a data point: [ 1440084737, 4, 0 ].
  
  Note that time is given in a Unix format called Epoch, which is a
  nonnegative integer holding the number of seconds that have elapsed
  since 00:00:00 UTC, Thursday, 1 January 1970.
  
  Given an array, data, of data points, write a function
  findBusiestPeriod that returns the time at which the mall reached its
  busiest moment last year. The return value is the timestamp, e.g.
  1480640292. Note that if there is more than one period with the same visitor peak, return the earliest one.
  
  Assume that the array data is sorted in an ascending order by the
  timestamp.


"""
input:  data = [ [1487799425, 14, 1], 
                 [1487799425, 4,  0],
                 [1487799425, 2,  0],
                 [1487800378, 10, 1],
                 [1487801478, 18, 0],
                 [1487801478, 18, 1],
                 [1487901013, 1,  0],
                 [1487901211, 7,  1],
                 [1487901211, 7,  0] ]

output: 1487800378 # since the increase in the number of people
                   # in the mall is the

"""  

def find_busiest_period(data):

  people = 0 
  max_time = 0
  max_people = 0
  for i in range(len(data)):

    if data[i][2] == 1:
      people += data[i][1]
    else:
      people -= data[i][1]

    if (i &lt; len(data)-1 and data[i][0] == data[i+1][0]):
      continue

    if people &gt; max_people:
      max_people = people
      max_time = data[i][0]
  return max_time 




data = [ [1487799425, 14, 1], 
                 [1487799425, 4,  0],
                 [1487799425, 2,  0],
                 [1487800378, 10, 1],
                 [1487801478, 18, 0],
                 [1487801478, 18, 1],
                 [1487901013, 1,  0],
                 [1487901211, 7,  1],
                 [1487901211, 7,  0] ]



test = find_busiest_period(data)
print(test)
Algorithm
I don't understand the purpose of comparing the timestamp of the next datum here:

if (i &lt; len(data)-1 and data[i][0] == data[i+1][0]):
  continue


The two events happened during the same second, but it's reasonable to assume that they are ordered, and therefore we should consider the total within that single second, unless the problem statement says otherwise.
Without that constraint, we have no need for the index i, and can consider just the members of the input data; we can give the elements meaningful names:
  for time,quantity,direction in data:

Now, we know we won't find a new maximum when people are exiting (assuming we're not given negative numbers of people), so we can move the test into the += branch:
    if direction == 1:
      # Some people entered
      people += quantity
      # Have we reached a new maximum?
      if people &gt; max_people:
        max_time, max_people = time, people
    elif direction == 0:
      # Some people left
      people -= quantity
    else:
      raise ValueError(direction)

General review

PEP8 recommends four spaces per indent level.

This doc-comment is both incomplete and incorrect:

&quot;&quot;&quot;
output: 1487800378 # since the increase in the number of people
                   # in the mall is the
&quot;&quot;&quot;



The doc-comment is in the wrong place (it should be just within the function body).

We should use a main guard.

Consider using doctest to provide more test cases.



Improved code
def find_busiest_period(data):
    &quot;&quot;&quot;
    Find the timestamp when the greatest number of people
    are in the building.

    &gt;&gt;&gt; find_busiest_period([]) is None
    True

    &gt;&gt;&gt; find_busiest_period([ [0, 0, 2] ])
    Traceback (most recent call last):
        ...
    ValueError: 2

    &gt;&gt;&gt; find_busiest_period([ [0, -5, 0] ])
    Traceback (most recent call last):
        ...
    ValueError: -5

    &gt;&gt;&gt; find_busiest_period([ [0, 5, 1], [2, 5, 1], [3, 5, 0] ])
    2

    &gt;&gt;&gt; find_busiest_period([ [1487799425, 14, 1], \
                              [1487799425, 4,  0], \
                              [1487799425, 2,  0], \
                              [1487800378, 10, 1], \
                              [1487801478, 18, 0], \
                              [1487801478, 18, 1], \
                              [1487901013, 1,  0], \
                              [1487901211, 7,  1], \
                              [1487901211, 7,  0] ])
    1487901211
    &quot;&quot;&quot;  
    people = 0 
    max_time = None
    max_people = 0

    for time,quantity,direction in data:
        if quantity &lt; 0:
            raise ValueError(quantity)
        if direction == 1:
            # Some people entered
            people += quantity
            # Have we reached a new maximum?
            if people &gt; max_people:
                max_time, max_people = time, people
        elif direction == 0:
            # Some people left
            people -= quantity
        else:
            raise ValueError(direction)

    return max_time 


if __name__ == &quot;__main__&quot;:
    import doctest
    doctest.testmod()