SDET Interview QA
Question 1: Given an array of integers, write a Java function to find the two numbers that add up to a given target sum.
Answer:
public int[] findTwoSum(int[] nums, int target)
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{-1, -1}; // If no two numbers add up to the target
}
{
Question 2: Implement a Java method to find the intersection of two arrays.
Answer:
public int[] findIntersection(int[] arr1, int[] arr2)
HashSet<Integer> set1 = new HashSet<>();
for (int num : arr1) {
set1.add(num);
}
HashSet<Integer> intersection = new HashSet<>();
for (int num : arr2) {
if (set1.contains(num)) {
intersection.add(num);
}
}
int[] result = new int[intersection.size()];
int i = 0;
for (int num : intersection) {
result[i++] = num;
}
return result;
}
{
Question 3: Write a Java function to determine if a given string is an anagram of another string.
Answer:
public boolean isAnagram(String str1, String str2)
if (str1.length() != str2.length()) {
return false;
}
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
{
Question 4: Implement a Java method to check if a given number is a prime number.
Answer:
public boolean isPrime(int num)
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
{
Question 5: Write a Java function to reverse a linked list.
Answer:
public ListNode reverseLinkedList(ListNode head)
ListNode prev = null;
ListNode curr = head;
ListNode next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
{
Question 6: Write a Java code snippet to send a GET request using RestAssured and validate the response status code.
Recommended by LinkedIn
Answer:
import static io.restassured.RestAssured.*
public class RestAssuredExample {
public static void main(String[] args) {
// Send GET request and validate response status code
given()
.when()
.get("https://api.example.com/users")
.then()
.assertThat()
.statusCode(200);
}
}
;
Question 7: Implement a Java method using RestAssured to send a POST request with a JSON payload and validate the response body.
Answer:
import io.restassured.http.ContentType
import static io.restassured.RestAssured.*;
public class RestAssuredExample {
public static void main(String[] args) {
// Prepare JSON payload
String jsonPayload = "{\"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}";
// Send POST request with JSON payload and validate response body
given()
.contentType(ContentType.JSON)
.body(jsonPayload)
.when()
.post("https://api.example.com/users")
.then()
.assertThat()
.statusCode(201)
.body("name", equalTo("John Doe"))
.body("email", equalTo("john.doe@example.com"));
}
}
;
Question 8: Write a Java method using RestAssured to extract and validate a specific value from a JSON response.
Answer:
import static io.restassured.RestAssured.*
import static org.hamcrest.Matchers.*;
public class RestAssuredExample {
public static void main(String[] args) {
// Send GET request and validate a specific value from the JSON response
given()
.when()
.get("https://api.example.com/users/123")
.then()
.assertThat()
.statusCode(200)
.body("name", equalTo("John Doe"))
.body("age", greaterThan(18));
}
}
;
Question 9: Implement a Java method using RestAssured to send a DELETE request and validate the absence of a resource.
Answer:
import static io.restassured.RestAssured.*
import static org.hamcrest.Matchers.*;
public class RestAssuredExample {
public static void main(String[] args) {
// Send GET request and validate response headers
given()
.when()
.get("https://api.example.com/users")
.then()
.assertThat()
.statusCode(200)
.header("Content-Type", containsString("application/json"))
.header("Cache-Control", equalTo("max-age=3600"));
}
}
;
Question 10: Write a Java method using RestAssured to extract and validate the response headers.
Answer:
import static io.restassured.RestAssured.*
import static org.hamcrest.Matchers.*;
public class RestAssuredExample {
public static void main(String[] args) {
// Send GET request and validate response headers
given()
.when()
.get("https://api.example.com/users")
.then()
.assertThat()
.statusCode(200)
.header("Content-Type", containsString("application/json"))
.header("Cache-Control", equalTo("max-age=3600"));
}
}
;
Question 9: Wrong solution
Very Nice Rishabh..