Round 1:
Tell me about yourself
Difference between hashmap and concurrent Hashmap
Scenario when to use concurrent hash map and when to use Hashmap
Programs:
Program Variation: Find next second greater number of given number
Input2: Number = 5687 (Next formed number are: 5768, 5786,....)
output: 5786
My Approach 1:
Boolean isAnagram(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2); //o(n log n)
if(c1 ==c2) return true;
else return false;
}Approad 2:
Boolean isAnagram(String s1, String s2 ) {
if(s1 == null || s2 ==null)
return false;
if(s1.length != s2.length)
return false;
Map<Character, Integer> mp = new HashMap<>();
int count=1;
for(int i=0; i<s1.length; i++) {
if(!mp.containsKey(s1.charAt(i))) {
mp.put(s1.charAt(i), count); //mp =[(m,2), (a,2), (d,1) ]
} else {
mp.put(s1.charAt(i), mp.get(s1.charAt(i))++);
}
}
for(int j=0; j<s2.length; j++) {
if(mp.containsKey(s2.charAt(j))) {
if(mp.get(s2.getCharAt(j)) > 1) {
mp.put(s2.charAt(j), mp.get(s2.charAt(j))--);
//mp =[(a,1)]
} else
mp.remove(s2.charAt(j));
}
}
return mp.isEmpty() ? true : false;
}3) Do you know what is blockingQueue ? Write a program to implement the same.
My Solution:
public class BlockingQueue {
private List<E> queue = new LinkedList<>();
int limit = 10;
BlockingQueue(int limit){
this.limit = limit;
}
synchronized offer(E data, long timeout){
while(this.queue.size() == this.limit) {
wait(timeout);
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(data);
}
synchronized E poll(long timeout)
{
while(this.queue.size() == 0){
wait(timeout);
}
if(this.queue.size() == this.limit) {
notifyAll();
}
queue.remove(0);
}
}Round 2:
Introduce yourself
Programing Question:
Given an array indicating different streams received voting. Return the winner stream name who has recieved maximum number of votes. In case of conflict in voting numbers, return the one whose won first.
arr = ["A", "B", "C", "B", "C", "D", "C", "D", "B"]
Output: C
My output: Working
import java.util.*;
public class findWinnerStreams {
public static void main(String[] args) {
final List<String> votes = Arrays.asList("A", "B", "C", "B", "C", "D", "C", "D", "B");
System.out.println("Winner stream:: "+findWinnerStreams(votes));
}
public static String findWinnerStreams(final List<String> votes) {
if(votes.size() == 1)
return votes.get(0);
if(votes.size() == 0)
return "";
Map<String, Integer> mp = new LinkedHashMap<>();
int count=0;
int max = 0; String maxVote = "";
for (String key: votes) {
if(!mp.containsKey(key))
mp.put(key, 1);
else {
count = mp.get(key);
count++;
if(max < count) { //use if(max<=count) if output required is B
max = count;
maxVote = key;
}
mp.put(key, count);
}
}
System.out.println(mp.toString());
return maxVote;
}
}Round 3:
This round was scheduled for 1 hour but was extended to 3 hours. but it was worth. I got selected after this round.
Q1) Given below program. What will be an output for given program and steps java will load and execute below program. Here we spoke about jvm classloading steps, lib loading, inintialization etc. (Google: steps on how jvm executes given program)
class Student {
int rollno;
String name;
static String college ="ITS";
Student(int r,String n){
rollno = r;
name = n;
}
void display () {
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[]){
System.out.println(college);
Student s = new Student(111,"Karan");
Student s2 = new Student(222,"Arjun");
s.display(); //”ITS”
s2.college = “NOT”;
s.display(); “NOT”
}
}Questions based on above program:
Q2) String testString = “efgabcdaabebaadcbaijkkjihfgv”
String output = “abcdaabebaadcba” is the longest palindromic subsequence.
Write a program for the above output.
It's similar to https://leetcode.com/problems/longest-palindromic-substring/
My output (Working fully):
public class Algo_LongestPlaindrome {
public static void main(String[] args) {
System.out.println(longestPalindrome("efgabcdaabefbaadcbaijkkjihfgv"));
}
//time: o(n * 2n)
static String longestPalindrome( String s) {
int maxLength = 0, len=0;
int start=0;
for(int i=0; i<s.length(); i++) {
int j=i;
len = Math.max(palindromeMaxLength(i, i, s), palindromeMaxLength(i, i+1, s));
if(maxLength < len) {
maxLength = len;
start = (i-(len-1)/2);
}
}
return s.substring(start, start+maxLength);
}
static int palindromeMaxLength(int i, int j, String s) {
int len =0;
if(j > s.length() || i<0)
return 0;
if(i == j) len = -1;
while( i >=0 && j<s.length()) {
if(s.charAt(i) != s.charAt(j))
break;
len= len+2;;
i--;
j++;
}
return len;
}}
Q3) Design your own flix (like netflix) for given features, decide which technnology will be best and define modules
- Screen movie: 10(initially)
- Search for movie
- Login/ register
- Licensing chk(premium)
- Logged in:last login
- Logest and large time
- Rating
- recommendation
- Payment gateway
My design:
Models:
Technology: Nosql, elastic search, spring boot, spring batch
Model with attributes:
User:
Id, name, emailId, pwd, premiumAccess(Y/N), lastAccessed, createdTime, updatedTime
Movie:
movieId, name, type, catalog, path, CreatedTime, UpdatedTime, needPremium, viewCount, avgRating
User Rating & review & recommendation engine
Rating:
Id, userId, movieId, rating(/5), creatingTime
Recommedation:
Id, userid, movieId, title, desc, suggested?
Flix recommendation engine:
UserWatched:
Id, userId, movieid, CountOfVisited
UserMovie:
Id, userid, MovieId, premiumValid, TimeWatched, whenWatched