Python challenge with timestamp












-1












$begingroup$


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 < len(data)-1 and data[i][0] == data[i+1][0]):
continue

if people > 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)









share|improve this question











$endgroup$

















    -1












    $begingroup$


    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 < len(data)-1 and data[i][0] == data[i+1][0]):
    continue

    if people > 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)









    share|improve this question











    $endgroup$















      -1












      -1








      -1





      $begingroup$


      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 < len(data)-1 and data[i][0] == data[i+1][0]):
      continue

      if people > 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)









      share|improve this question











      $endgroup$




      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 < len(data)-1 and data[i][0] == data[i+1][0]):
      continue

      if people > 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)






      python interview-questions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 mins ago









      Jamal

      30.3k11117227




      30.3k11117227










      asked 4 hours ago









      NinjaGNinjaG

      762427




      762427






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212491%2fpython-challenge-with-timestamp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212491%2fpython-challenge-with-timestamp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin