Can You Access HackerRank Solution

The program reads an integer from the user and checks if it is a power of 2. It uses the concept of inner classes in Java, where an inner class is defined within the scope of another class. The program also includes a mechanism to prevent the termination of the JVM using the 'exit' function.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Can You Access HackerRank Solution HackerRank solution using CodeRankGPT:

The program first forbids the JVM exit using a custom SecurityManager. It then reads an integer from the user and creates an instance of the inner class 'Private' within the 'Inner' class. The 'powerof2' method of the 'Private' class checks if the number is a power of 2 by performing a bitwise AND operation between the number and one less than the number. If the result is zero, the number is a power of 2. The result is then printed to the console.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.advanced;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.Permission;

/**
 *
 */
public class CanYouAccess {
	public static void main(String[] args) throws Exception {
		DoNotTerminate.forbidExit();

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			int num = Integer.parseInt(br.readLine().trim());
			Object o;// Must be used to hold the reference of the instance of the class
						// Solution.Inner.Private

			// Inner i= new Inner();
			// Private p=new Private();
			/**main code starts from here*/
			CanYouAccess.Inner si = new CanYouAccess.Inner();
			o = si.new Private();
			System.out.println(num + " is " + ((CanYouAccess.Inner.Private) o).powerof2(num));
			/**main code end  here*/

			System.out.println("An instance of class: " + o.getClass().getCanonicalName() + " has been created");

		} // end of try

		catch (DoNotTerminate.ExitTrappedException e) {
			System.out.println("Unsuccessful Termination!!");
		}
	}// end of main

	static class Inner {
		private class Private {
			private String powerof2(int num) {
				return ((num & num - 1) == 0) ? "power of 2" : "not a power of 2";
			}
		}
	}// end of Inner

}// end of Solution

class DoNotTerminate { // This class prevents exit(0)

	public static class ExitTrappedException extends SecurityException {

		private static final long serialVersionUID = 1L;
	}

	public static void forbidExit() {
		final SecurityManager securityManager = new SecurityManager() {
			@Override
			public void checkPermission(Permission permission) {
				if (permission.getName().contains("exitVM")) {
					throw new ExitTrappedException();
				}
			}
		};
		System.setSecurityManager(securityManager);
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform