Recyclops's blog

By Recyclops, history, 23 months ago, In English

I couldn't find any discussion or editorial blog, so am creating a new one.
Was able to solve the 2nd problem with Segment Tree (standard stuff) and attempted to solve the first one with 6D DP (wasn't able to debug an RE).
It would be great if you could share your approaches for the four problems.

  • Vote: I like it
  • +39
  • Vote: I do not like it

| Write comment?
»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Do you have questions? Can you post?

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    No, I don't have them. Thought they'd post an editorial or something.
    The IDE was kind of unintuitive as well, had to rewrite more than 60 lines of code because the text-area wouldn't accept any text that I had written in my editor.
    Plus there was a grey area around whether one could create additional functions and classes, or only the solution function could be modified.
    The testing apparatus could have also been better.
    Overall, not a great experience.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +7 Vote: I do not like it

      I also hate the IDE of InterviewBit. Worst experience I have with the IDE.

»
23 months ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

First Problem___

You are given a range l,r. We have to count total no of beautiful numbers in that range.

Numbers are beautiful if, xor of all digits of that number is greater than the avg(max digit of that number + min digit of that number)

return result%1e9+7

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Could someone share the intended approach / an elegant solution?
    Mine was clumsy and complicated and full of bugs; 6D DP's never a good idea, ig.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      Mine was 5D digit dp and cancerous as it involved finding the maximum and least set bit of a number. The 6d digit dp is better and easier to write.

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Did u get ac for this?

        • »
          »
          »
          »
          »
          23 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          yes

          • »
            »
            »
            »
            »
            »
            23 months ago, # ^ |
              Vote: I like it +1 Vote: I do not like it

            can you share the solution of 6-D dp?

          • »
            »
            »
            »
            »
            »
            23 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            do you have any idea what will be the final cutoff for the shortlisting process?

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Could you please share the DP approach?

    • »
      »
      »
      23 months ago, # ^ |
      Rev. 3   Vote: I like it +4 Vote: I do not like it
      Mine is 6D Dp. Could not find the bug in contest time Later found it.
    • »
      »
      »
      23 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I dont know what is wrong with my code, but I have used 5-D dp and used digit DP . Is there any silly mistake? or some wrong approach?

      int dp[18][10][10][16][2];
      const int N = 1e9+7;
      int get(int pos , string &s, int mx, int mn, int xr, int tgt){
          if(pos == 0) return (2*xr>mx+mn);
          if(dp[pos][mx][mn][xr][tgt] != 1) return dp[pos][mx][mn][xr][tgt];
      
          int ans = 0, ub = (tgt? (s[s.size()-pos]-'0'):9);
          for(int i = 0; i <= ub; i++){
              ans += get(pos-1,s,max(mx,i),min(mn,i),(xr^i),tgt&(i==ub));
              ans %=N;
          }
          return dp[pos][mx][mn][xr][tgt] = ans;
      }
      int Solution::solve(string A, string B){
          memset(dp,-1,sizeof(dp));
          int a = get(A.size(),A,0,9,0,1);
          memset(dp,-1,sizeof(dp));
          int b = get(B.size(),B,0,9,0,1);
          int mx=0,mn=9,xr=0;
          for(auto it: A)mx=max(mx,it-'0'),mn=min(mn,it-'0'),xr^=(it-'0');
          if(2*xr>mx+mn)ans--;
          return ans;
      }
      
      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        You need one more state known to handle cases where you haven't started building numbers. For example if you start building from the second digit and put a 0 there it is not considered a part of the number as it is a leading zero.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What was the constraint over l and r

»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Btw, what is the selection criteria for the Online Round?
Is it based on the number of problems solved, or quality of submissions, or something else?
There wasn't even a rankings page, afaik.

  • »
    »
    23 months ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Ig first they will go with number of problems with 100% acceptance rate, then quality of code. Then rest others.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think they take various factors into account like your resume quality, college, CGPA, branch etc.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    its based on points scored

  • »
    »
    23 months ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    I solved 3 and submitted 4th at the end (don't know if it was accepted or not as the test ended while compiling) and I got the clearance of Online Round mail.
    The ranking should be the only selection criteria I think which will be based on the points scored, in case of a tie, time will be considered.

    • »
      »
      »
      23 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      From Interview-Bit or from Trilogy?
      I have received one for successful completion from Interview-Bit.
      Don't know if it's in any way related to the actual selection process.

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        From Trilogy, for completing the aptitude test as the next round.

»
23 months ago, # |
  Vote: I like it +2 Vote: I do not like it

first was digit dp, second was usual prefix sum, third I did using Manachers algo.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Manacher's Algo?
    Just found something new to learn.
    Thought of applying KMP or ZF in some way, but knew it wouldn't work.

  • »
    »
    23 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    which one you did by prefix sum?the problem in which we need to give x1^x2 where x1 and x2 are bitwise and of range l1,r1,l2 and r2?

    If yes how?I did it using seg trees.

    upd got it from a below comment.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      Simple method: We have to find the values of x1 and x2 then xor them. So, first we create a prefix array pref[i][j] of size n*32 which represents the sum of jth bit of the indices in the range [0, i]. Now, when we want to calculate value of x1, we find the number of set bits of the jth bit and check if it is equal to r1 — l1 + 1 (size), because for bitwise and to be 1, we need all the bits to be equal to 1. Similarly, we can find x1 and x2, then xor them

  • »
    »
    23 months ago, # ^ |
    Rev. 3   Vote: I like it +1 Vote: I do not like it

    Third problem can be also done by some simple string hashing and binary search.

    Make prefix and suffix hash using some mod value (I used 1e9+9). Now just binary search on the length uptil which the string [i,i+mid] from prefix hash and [i-mid,i] from suffix hash is same. This problem can be solved using binary search in log(n) time.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you please share some similar question like this?

»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it

3rd Problem

Given a string s and a vector Q containing queries. For each element i in Q we have to find the length of longest odd palindrome whose middle index is i in string s. 1 <= Len(s), Len(Q) <= 1e5. We have to return vector of results of each query

»
23 months ago, # |
  Vote: I like it +1 Vote: I do not like it

In second problem just store prefix of every bit for all element and check if bit[r][j] — bit[l-1][j] == r-l+1...if yes add that bit to answer. Calculate the same for both given range and store the xor of both!!

»
23 months ago, # |
  Vote: I like it +13 Vote: I do not like it

Here are the problems:

problem 1
problem 2
problem 3
problem 4
»
23 months ago, # |
  Vote: I like it +15 Vote: I do not like it

Here is my approaches...

Problem A
Problem B
Problem C
Problem D
  • »
    »
    23 months ago, # ^ |
      Vote: I like it +7 Vote: I do not like it

    Just an addition, C was basically a direct implementation of Manacher's algo, which gives linear runtime.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    did u solution for the problem c got ac i got tle for a few tc.My code get 286/300.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      This was my code for 3rd problem and I got an AC for that.

      def solve(s, b):
          n = len(s)
          s = '('+s+')'
          p = [0]*(n+1)
          l = 1
          r = 1
          for i in range(1, n+1):
              p[i] = max(0, min(r - i, p[l + (r - i)]))
              while(s[i-p[i]] == s[i+p[i]]):
                  p[i] += 1
              if(i + p[i] > r):
                  l = i - p[i]
                  r = i + p[i]
          ans = []
          for i in b:
              ans.append(p[i]*2-1)
          return ans
      
      
      s = "abacaba"
      b = [2, 3, 4]
      print(solve(s, b))
      
    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +4 Vote: I do not like it

      Yeah, my binary search + hashing solution gave 296/300 initially, then I removed the unecessary %mod operations during addition, i.e (a%m + b%m > m) ? (a%m + b%m — m) : (a%m + b%m). that was enough for my submission to then get a full score.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can You Please share Your approach for problem C of binary search with hashing

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Sure! so I just hashed the string in both the directions i.e. forward and backward/reverse. Then for a query of 'i' I searched for the maximum length that has the center 'i' and is odd. i.e. maximum x for [i — x... i + x] is palindrome. you may check palindrome nature by taking hash between [i — x .... i] for reverse hash table and [i ... i + x] for forward hash table and just compare the result. They should be equal for a substring being palindrome!

  • »
    »
    23 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    In C, in test cases where the density of palindromes is high, wouldn't the effective complexity of Polynomial Hashing with Verification on Match be O(N*(LogN)*N)?
    The probability of hash collision is extremely low, I know, especially if a large prime is used, but still, without verification wouldn't it be non-deterministic?

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ig test cases in problem C are weak

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      Usually if we do single hash for a string with (p = 31, mod = 1e9 + 7) it don't get collision for a string of length N <= 1e5 but may fail for N <= 1e6 (would need double hash there).

      And I optimised my code to work in O(N * log(N)) with some precomputations.

»
23 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Was anyone able to solve the 4th problem.. The expected value one? If yes please share your approach.

»
23 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Is solving 1 problem enough to get selection mail?

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I solved 2 problems(digit-dp and seg-tree) and got a mail for CCAT(Aptitude Test).

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      what was the criteria for the selection process.I solved 2nd, 3rd and 200 pts for 1st but no mail for interviews.

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        not sure either, I guess they also looked for code quality or something. I might be wrong.

»
23 months ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

As usual I fucked up CCAT yet another time!

Anyone who successfully cleared CCAT. Any tips?

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    have you received CCAT mail??

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yup! I guess everyone who were shortlisted had to complete ccat by 6pm ist 19 dec