Nitin Agrawal
Contact -
  • Home
  • Interviews
    • InterviewFacts
    • Resume Thoughts
    • Companies >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Samsung
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • Delphix
        • Fractal Enterprises LLP
        • CDK Global
        • Tide-Banking
        • Epic
      • ServiceBasedCompanies >
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • ZeMoSo
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Java14
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • AngularJS
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Design Patterns
    • AWS >
      • Honeycode
  • Databases
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search >
      • StudySources
  • Random issues
    • TOAD issue
    • Architect's suggestions
    • ApacheSpark Installation
  • Your Views

BlueOptima coding round

4/15/2018

2 Comments

 
In the coding round of this company, question was to find the longest substring having distinct characters.
​Below is my solution for this problem, though I was not able to complete this in the given time frame, as I was having around 35 mins remaining for this, you can also check the related code given in Interview2
But may be it can help you to crack this or other such interview -

import java.util.HashMap;
/**
 * Below question was asked to code in interview for BlueOptima.
 * Question was to find the largest substring with distinct characters.
 * Below is my solution, which I can think of for now with my a few below test cases.
 * Please let me know the case for which it doesn't work.
 * @author nitin
 *
 */
public class BlueOptima {

 public static void main(String args[]) {
  System.out.println(LargestSubString("abcda")); // 4
  System.out.println(LargestSubString("abadc")); // 4
  System.out.println(LargestSubString("abadcbefg")); // 7
  System.out.println(LargestSubString("abadcbeag")); // 6
  System.out.println(LargestSubString("abadcbeagb")); // 6
  System.out.println(LargestSubString("aaaaaaaaaa")); // 1
  System.out.println(LargestSubString("abcddceaghi")); // 7
  System.out.println(LargestSubString("abclmceaghi")); // 8
  System.out.println(LargestSubString("abcddceag")); // 5
  System.out.println(LargestSubString("abadcbeagbklmnop")); // 10
  System.out.println(LargestSubString("abcdefghijklmf")); // 13
 }

 static int LargestSubString(String S){
           int max = 0;
           HashMap<Character, Integer> hm = new HashMap<>();
           char[] chars = S.toCharArray();
            int len = chars.length;
            int start = 0;
            for(int i = 0; i < len; i++) {
              char ch = chars[i];
              if(!hm.containsKey(ch)) {
                 hm.put(ch, i);
              } else {
           ​      int pos = hm.get(ch);
           ​      if(pos != start) {
           ​         if(hm.size() > max)
           ​             max = hm.size();
           ​             int k = hm.get(chars[i]) + 1;
           ​             hm.clear();     
           ​             start = k;
           ​        while(k < i) {
           ​            hm.put(chars[k], k);
           ​            k++;
           ​        }
                 } else {
                start = pos + 1;
                int k = pos;
                while(k >= 0) {
           ​         if(hm.containsKey(chars[k]) && hm.get(chars[k]) == k)
           ​             hm.remove(chars[k]);
           ​             k--;
           ​        }
           ​     }
               hm.put(ch, i);
               }
             }
             if(hm.size() > max)
           ​       max = hm.size();
             return max;
         ​   }
       }
2 Comments
Harsh Fadadu
8/16/2019 09:50:44 pm

public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}

Reply
Nitin Agrawal
8/16/2019 10:10:17 pm

Hello Harsh,
Thanks for the better & clean solution.

Reply



Leave a Reply.

    Author

    Nitin Agrawal

    Archives

    April 2018

Powered by Create your own unique website with customizable templates.