Hi Friends,
In this post, I'm sharing interview questions asked in Virtusa Polaris.
Question 1:
Find frequency of a character and if more than one characters have same frequency then find character with more ASCII value.
Answer:
Algorithm to solve this problem:
Question 2:
Implement spiral movement in 2D array.
Answer:
Left to Right:
Move variable i from rowStart till colLength. Print data from first row till last column.
Top to Bottom:
Move variable i from (rowStart+1) till rowLength. Print data in last column.
We need to start from rowStart+1, because we already printed corner element in Left to Right printing and no need to include it again. Same treatment for corner elements in other directions.
Right to Left:
Move variable i from colLength - 1 till colStart. Print data in last row.
Bottom to Up:
Move variable i from rowLength - 1 till rowStart. Print data in first column.
After printing all 4 directions , in next iteration, we need to start from second row and second column , so increment rowStart++ and colStart++.
We need to print till second last column and till second last row , so decrement (colLength--) and (rowLength--).
Question 3:
What are the approaches for REST API versioning ?
Answer:
There are multiple approaches for doing versioning in REST API.
Important ones are described below:
URI Versioning:
Using the URI is the most straight forward approach[and also most commonly used]. Though it does violate the principle that URL should refer to a unique resource.
http://api.example.com/v1
http://apiv1.example.com
Versioning using custom Request Header:
e.g.:
Accept-version : v1
Accept-version : v2
Versioning using Accept Header:
e.g.:
Accept : application/vnd.example.v1+json
Accept : application/vnd.example+json;version=1.0
Question 4:
Explain Builder Design pattern and in which scenario it is used?
Answer:
What problem does it solve?:
Question 5:
Difference between split() and StringTokenizer class?
Answer:
split() vs StringTokenizer:
Using StringTokenizer class, we have the option to use multiple delimiters with the same StringTokenizer object. But that's not possible with split() method.
split() method in String class is more flexible and easy to use. But StringTokenizer class is faster than split() method.
e.g.:
StringTokenizer st = new StringTokenizer("a:b:c" , ":");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
StringTokenizer with multiple identifiers:
In this post, I'm sharing interview questions asked in Virtusa Polaris.
Question 1:
Find frequency of a character and if more than one characters have same frequency then find character with more ASCII value.
Answer:
Algorithm to solve this problem:
- Convert String into char array and take a HashMap, count variable and a char variable.
- Iterate this array.
- for every character, store it inside HashMap as key and with it's frequency as value. Also increase it's frequency value by 1. Along with that compare this frequency with count variable and if it is > count , then update count with frequency and also update char variable with that specific character for which we have updated count variable.
- If frequency = count, then check if new character's ASCII code is > ASCII of char. If it is, the update char with new character.
- At last, we will have the required character in char variable with more frequency or with more ASCII value, in case some other char has same frequency.
Question 2:
Implement spiral movement in 2D array.
Answer:
Left to Right:
Move variable i from rowStart till colLength. Print data from first row till last column.
Top to Bottom:
Move variable i from (rowStart+1) till rowLength. Print data in last column.
We need to start from rowStart+1, because we already printed corner element in Left to Right printing and no need to include it again. Same treatment for corner elements in other directions.
Right to Left:
Move variable i from colLength - 1 till colStart. Print data in last row.
Bottom to Up:
Move variable i from rowLength - 1 till rowStart. Print data in first column.
After printing all 4 directions , in next iteration, we need to start from second row and second column , so increment rowStart++ and colStart++.
We need to print till second last column and till second last row , so decrement (colLength--) and (rowLength--).
Question 3:
What are the approaches for REST API versioning ?
Answer:
There are multiple approaches for doing versioning in REST API.
Important ones are described below:
URI Versioning:
Using the URI is the most straight forward approach[and also most commonly used]. Though it does violate the principle that URL should refer to a unique resource.
http://api.example.com/v1
http://apiv1.example.com
Versioning using custom Request Header:
e.g.:
Accept-version : v1
Accept-version : v2
Versioning using Accept Header:
e.g.:
Accept : application/vnd.example.v1+json
Accept : application/vnd.example+json;version=1.0
Question 4:
Explain Builder Design pattern and in which scenario it is used?
Answer:
What problem does it solve?:
- Class constructor requires a lot of information.
So, whenever we have an immutable class, then we need to pass all the information/parameters inside the constructor of the class.
When to use Builder Pattern:
- When we have a complex process to construct an object involving multiple steps, then builder design pattern can help us.
- In builder, we remove the logic related to object construction from "client" code and abstract it in separate classes.
Question 5:
Difference between split() and StringTokenizer class?
Answer:
split() vs StringTokenizer:
Using StringTokenizer class, we have the option to use multiple delimiters with the same StringTokenizer object. But that's not possible with split() method.
split() method in String class is more flexible and easy to use. But StringTokenizer class is faster than split() method.
e.g.:
StringTokenizer st = new StringTokenizer("a:b:c" , ":");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
StringTokenizer with multiple identifiers:
StringTokenizer st = new StringTokenizer("http://100.90.80.3/", "://.");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
Output:
100
90
80
3
Using split() method:
for(String token : "a:b:c".split(":")){
System.out.println(token);
}
Question 6:
Why String is immutable in java?
Answer:
String is immutable due to multiple reasons:
- Due to String Pool facility. The String Pool is managed by String class internally.
- Due to Network Security, as URL is sent in String format.
- Due to thread security. Strings can be shared among multiple threads.
- Class loading mechanism
- Immutability allows string to store it's hashcode and we don't need to calculate hashcode every time we call hashcode() method, which makes it very fast as hashmap keys to be used in HashMap in java.
Question 7:
Why the variables defined in try block cannot be used in catch or finally?
Answer:
When we define any variable in try block and also use that variable in catch or finally, then suppose some exception occurs in try block before the line where that variable is defined. In that case, control goes to catch or finally and we will be using undefined variable in these blocks, because exception occured before the declaration.
That's why variables defined in try block cannot be used in catch or finally.
That's all from this interview.
Hope this post help everybody clearing java interviews.
Thanks for reading.
No comments:
Post a Comment