My Software Engineering Internship Interview Experience
What I Prepared for Vs What I Actually Got !
I’ve listed a few questions that stood out to me as out-of-the-box questions(for me).
1. There are two lists: one has 50 numbers and the other list has 75 numbers. Write code in Java to find the common elements of these two lists
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CommonElements {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
// Add more numbers to list1
for (int i = 4; i <= 50; i++) {
list1.add(i);
}
List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(3);
list2.add(4);
// Add more numbers to list2
for (int i = 5; i <= 75; i++) {
list2.add(i);
}
List<Integer> commonElements = findCommonElements(list1, list2);
System.out.println("Common elements: " + commonElements);
}
public static List<Integer> findCommonElements(List<Integer> list1, List<Integer> list2) {
Set<Integer> set = new HashSet<>(list1);
set.retainAll(list2);
return new ArrayList<>(set);
}
}
In this example, the findCommonElements
method takes two lists as input and uses a HashSet
to efficiently find the common elements. The retainAll
method of the HashSet
modifies it to contain only the common elements between the two lists. Finally, the common elements are converted back to a List
for easy printing.
- Importing Required Classes: We import
ArrayList
,HashSet
,List
, andSet
to use these data structures. - Main Method:
- We create two lists,
list1
andlist2
. - We add a few numbers to
list1
andlist2
. - For demonstration purposes, I added loops to populate
list1
with numbers from 1 to 50 andlist2
with numbers from 2 to 75.
3. Finding Common Elements:
- The
findCommonElements
method creates aHashSet
fromlist1
. - It then uses
retainAll
to keep only the elements that are also inlist2
. - Finally, it converts the set back to a list and returns it.
4. Printing Common Elements: The common elements are printed to the console.
This code is straightforward and leverages the retainAll
method to find the intersection of the two sets, which is efficient and easy to understand.
2. how to do this in python
def find_common_elements(list1, list2):
set1 = set(list1)
set2 = set(list2)
common_elements = set1 & set2
return list(common_elements)
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
common_elements = find_common_elements(list1, list2)
print("Common elements:", common_elements)
In this Python code, the find_common_elements
function takes two lists as input, converts them to sets, and then uses the &
(intersection) operator to find the common elements between the sets. The result is then converted back to a list for printing.
3. What is abstraction and encapsulation?
- I answered this question, and this was my interviewer’s response.
- The definition you provided for abstraction seems to match encapsulation, right? In abstraction, we don’t show the HOW and WHY part.
Answer:
Abstraction and encapsulation are related concepts in object-oriented programming but serve different purposes:
- Abstraction: Abstraction refers to the process of hiding the complex implementation details and showing only the essential features of the object. It focuses on WHAT an object does rather than HOW it does it. In abstraction, we create a simplified view of an object by focusing on its essential characteristics and ignoring unnecessary details. For example, a car can be abstracted as a vehicle with properties such as speed, fuel level, and direction, without delving into the internal combustion engine mechanics.
- Encapsulation: Encapsulation, on the other hand, is about bundling the data (attributes) and methods (functions) that operate on the data into a single unit, called a class. It involves keeping the internal state of an object private and providing controlled access to the object’s properties through methods. Encapsulation helps in achieving data hiding and protecting the integrity of an object’s data. For example, in a bank account class, the balance and transaction methods are encapsulated within the class to ensure that the balance is updated securely and accessed only through defined methods.
While abstraction focuses on the outside view of an object and its behavior, encapsulation ensures that the object’s internal workings are hidden and accessed only through well-defined interfaces. These principles contribute to making code more modular, maintainable, and secure in object-oriented programming.
4. Given variables a = 15
and b = 7
, provide a mathematical method to swap their values, expressing a
in terms of b
and b
in terms of a
.
To swap the values of a
and b
using mathematical expressions:
- Express
a
in terms ofb
: a=a+b−(b=a)a = a + b - (b = a)a=a+b−(b=a) - Express
b
in terms ofa
: b=a+b−(a=b)b = a + b - (a = b)b=a+b−(a=b)
5. Given a table with columns name
and salary
, write an SQL query to find the third highest salary.
To find the third highest salary from the salaries
table:
SELECT DISTINCT salary
FROM salaries
ORDER BY salary DESC
LIMIT 2, 1;
- SELECT DISTINCT salary: Selects unique salary values from the
salaries
table. - ORDER BY salary DESC: Orders the salaries in descending order so that the highest salary comes first.
- LIMIT 2, 1: Skips the first two highest salaries (since
LIMIT 2
skips 0th and 1st highest) and then selects the next one (which is the third highest salary).
This query effectively filters out the first and second highest salaries, leaving us with the third highest salary from the table. Adjust the table and column names (salaries
and salary
) as per your actual database schema.
Here are a few pointers for you to make not just a good, but a great impression on the interviewer.
- Make sure to maintain eye contact, even though it’s online. They actually notice it.
- If you are wondering why, even though you have applied to 100+ or maybe even 1000+ companies, you still haven’t received any replies, have trust in yourself and your work. You will get that call.
- For the “Tell me your weaknesses” question, give a weakness that shows some positivity. Don’t mention a weakness that will affect your job. Say something like public speaking or pitching ideas in a short time.
- Start the interview with a confident handshake and end it with a solid handshake.
- Keep a smiling and enthusiastic face throughout the interview.
- If you’re stuck on a problem, always ask if you can write it down so you can align the elements.