Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:11:45+00:00 2026-06-02T01:11:45+00:00

I found this interview question, and I couldn’t come up with an algorithm better

  • 0

I found this interview question, and I couldn’t come up with an algorithm better than O(N^2 * P):

Given a vector of P natural numbers (1,2,3,…,P) and another vector of length N whose elements are from the first vector, find the longest subsequence in the second vector, such that all elements are uniformly distributed (have the same frequency).

Example : (1,2,3) and (1,2,1,3,2,1,3,1,2,3,1). The longest subsequence is in the interval [2,10], because it contains all the elements from the first sequence with the same frequency (1 appears three times, 2 three times, and 3 three times).

The time complexity should be O(N * P).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:11:47+00:00Added an answer on June 2, 2026 at 1:11 am

    “Subsequence” usually means noncontiguous. I’m going to assume that you meant “sublist”.

    Here’s an O(N P) algorithm assuming we can hash (assumption not needed; we can radix sort instead). Scan the array keeping a running total for each number. For your example,

      1  2  3
     --------
      0  0  0
    1 
      1  0  0
    2
      1  1  0
    1
      2  1  0
    3
      2  1  1
    2
      2  2  1
    1
      3  2  1
    3
      3  2  2
    1
      4  2  2
    2
      4  3  2
    3
      4  3  3
    1
      5  3  3
    

    Now, normalize each row by subtracting the minimum element. The result is

     0: 000
     1: 100
     2: 110
     3: 210
     4: 100
     5: 110
     6: 210
     7: 100
     8: 200
     9: 210
    10: 100
    11: 200.
    

    Prepare two hashes, mapping each row to the first index at which it appears and the last index at which it appears. Iterate through the keys and take the one with maximum last – first.

    000: first is at 0, last is at 0
    100: first is at 1, last is at 10
    110: first is at 2, last is at 5
    210: first is at 3, last is at 9
    200: first is at 8, last is at 11
    

    The best key is 100, since its sublist has length 9. The sublist is the (1+1)th element to the 10th.

    This works because a sublist is balanced if and only if its first and last unnormalized histograms are the same up to adding a constant, which occurs if and only if the first and last normalized histograms are identical.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I found on a algorithm/interview question book, someone else had several
I found this interesting blog post via CodingHorror: My Favorite Interview Question . In
I faced this question in interview. When I searched on internet I found different
I need help in understanding this interview question: Q: Find an algorithm to find
This is an interview question that I found interesting. Write a method that takes
Found this interesting interview question: You need to display the sales data for your
Found this question that explained a way to communicate between layers. However there is
I found this question that is discussing what I would like to do, but
I found myself confronted with an interview question where the goal was to write
I asked a question like this in an interview for a entry level programmer:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.