public class DeadLock {
static class ThreadOne implements Runnable {
public void run()
{
synchronized (Integer.class)
{
System.out.println(Thread.currentThread().getName() + " - Got lock on Integer.class");
synchronized (String.class)
{
System.out.println(Thread.currentThread().getName() + " - Got lock on String.class");
}
}
}
}
static class ThreadTwo implements Runnable {
public void run()
{
synchronized (String.class)
{
System.out.println(Thread.currentThread().getName() + " - Got lock on String.class");
synchronized (Integer.class)
{
System.out.println(Thread.currentThread().getName() + " - Got lock on Integer.class");
}
}
}
}
}
import org.junit.jupiter.api.Test;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
assertTimeout(Duration.ofMillis(300), () -> {
new Thread(new DeadLock.ThreadOne(), "ThreadOne").start();
new Thread(new DeadLock.ThreadTwo(), "ThreadTwo").start();
});
}
}
We have natural sequesnce of numbers with a missed 1 element. How can we find the missing element
public class MissingInt {
public static int findMissingElement(int[] array) {
// write yor code here
int XOR = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] != 0) {
XOR ^= array[i];
}
XOR ^= (i + 1);
}
XOR ^= array.length + 1;
return XOR;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
int[] array1 = {1,2,3,4,5,7,8,9};
int expected = 6;
int actual = MissingInt.findMissingElement(array1);
assertEquals(expected, actual);
}
@Test
void test2() {
int[] array1 = {1,2,4,5,6,7,8,9};
int expected = 3;
int actual = MissingInt.findMissingElement(array1);
assertEquals(expected, actual);
}
}