+ 1

# logical puzzles java interview question

Q1 You are standing in a hallway next to three light switches switches, all of which are off. Each switch operates a different incandescent light bulb in the room at the end of the hall. You cannot see the lights from where the switches are. Determine which light corresponds to each switch. You may go into the room with the lights only once.

3rd Jul 2017, 5:55 AM
Hema Dhayalan
8 Respuestas
+ 2
import java.util.*; public class Program { public static void main(String[] args) { Scanner s = new Scanner(System.in); int x, n; while (true) { System.out.print("enter any of the switches: "); n = s.nextInt(); System.out.println("switch " + n + " is on for 20 mins and get off...\n"); System.out.print("now enter any of the two switches: "); x = s.nextInt(); // Constraints n != x, 1 <= n <= 3, 1 <= x <= 3 if (n != x && (n > 0 && n < 4) && (x > 0 && x < 4)) break; System.out.println("\nInvalid switches! Try again!\n"); } int warm = n; // the warm lamp int light = x; // the glowing lamp int cool = 0; // the cool lamp if ((warm == 2 && light == 3) || (warm == 3 && light == 2)) { cool = 1; } else if ((warm == 3 && light == 1) || (warm == 1 && light == 3)) { cool = 2; } else if ((warm == 1 && light == 2) || (warm == 2 && light == 1)) { cool = 3; } System.out.printf("%nSwitch-%d, which was turned on, " + "corresponding to the warm lamp.%n", n); System.out.printf("Switch-%d, which is turned on, " + "corresponding to the glowing lamp.%n", x); System.out.printf("Switch-%d, which was not touched, " + "corresponding to the cool lamp.%n", cool); } }
4th Jul 2017, 2:55 AM
Boris Batinkov
+ 1
is my program is right or not for that puzzle?
3rd Jul 2017, 7:19 AM
Hema Dhayalan
+ 1
Hi, there! The logic at the end is not right. You get always "true" in the condition. (cool = 0 and light = 0, and right away the condition cool == light, which always would evaluate to "true"). In another hand the code in the block would says always the right answer, that the cool light corresponds to the switch that is not touched. But like the condition says, lets determine which lights corresponds to each switch (as far as I understand it): We have 3 switches for 3 lamps. To find the corresponding lights of the switches we turn on the 'n' switch (let say it is №1) for 20 min. and then turning it off, so one of the lamps must be hot for a while. Then we turn on one of the other two switches 'x' (let say it is №2), and we already know the lamps, because now we have a three states of the lamps: warm, turned on, and cool. So: The hot lamp will correspond to the switch №1. The glowing lamp to switch №2. The cool lamp to switch №3. The condition: if ((warm == 2 && light == 3) || (warm == 3 && light == 2)) cool = 1; else if ((warm == 3 && light == 1) || (warm == 1 && light == 3)) cool = 2; else if ((warm == 1 && light == 2) || (warm == 2 && light == 1)) cool = 3;
4th Jul 2017, 2:55 AM
Boris Batinkov
+ 1
thank u... you are right..
4th Jul 2017, 4:32 AM
Hema Dhayalan
+ 1
thank u for your suggestion @Boris Batinkov
6th Jul 2017, 6:57 AM
Hema Dhayalan
+ 1
yo
24th Jul 2017, 6:41 PM
Speedo
0
import java.util.*; public class Program { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("enter any of the switch:"); int n=s.nextInt(); System.out.println(n); System.out.println("switch"+n+"is on for 20 mins and get off"); int x=s.nextInt(); System.out.println("now enter any of the two switches:"+x); switch(x){ case 1: System.out.println("light 1 is on when enter the room"); break; case 2: System.out.println("light 2 is on when enter the room"); break; case 3: System.out.println("ligth 3 is on when enter the room"); break; default: System.out.println("invalid switch"); } int cool=0; int light =0; if(light==cool){ System.out.println(" cool light corresponding to the switch which was not touched"); } else{ System.out.println("switch"+n+"was entered for that corresponding hot light"); } } }
3rd Jul 2017, 7:18 AM
Hema Dhayalan
0
http://codingbat.com/java I forgot to mention about this site, where the goal is to learn to code and to think logarithmically (to solve a problem piece by piece). There are a lot of exercises about conditions (boolean logic), strings, arrays and etc. They are well collected and arranged. Good luck, and happy coding!
4th Jul 2017, 12:16 PM
Boris Batinkov