yavuztas / java-quiz-common

Solutions for common java algorithm questions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solutions for common java algorithm questions

I have packed my java codes mainly used for online quizes before, to form a collection of solutions for common java algorithm and data structure questions. Note that Java 8 is required to build this project. Hope it helps to fellow developers!

Questions

1. Path: Path.java

Write a function that provides change directory (cd) function for an abstract file system.
Specifications:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as "..".
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions. For example:
Path path = new Path("/a/b/c/d");
path.cd('../x');
System.out.println(path.getPath());

should display '/a/b/c/x'.

2. Folders: Folders.java

Implement a function folderNames, which accepts a string containing an XML file that specifies folder structure and returns all folder names that start with startingLetter. The XML format is given in the example below.
For example, for the letter 'u' and an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<folder name="c">
    <folder name="program files">
        <folder name="uninstall information" />
    </folder>
    <folder name="users" />
</folder>

the function should return a collection with items "uninstall information" and "users" which can be any order.

3. Sorted Search: SortedSearch.java

Implement function countNumbers that accepts a sorted array of unique integers and, efficiently with respect to time used, counts the number of array elements that are less than the parameter lessThan.
For example, SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array elements less than 4.

4. Palindrome: Palindrome.java

A palindrome is a word that reads the same backward or forward.
Write a function that checks if a given word is a palindrome. Character case should be ignored.
For example, isPalindrome("Geleveleg") should return true as character case should be ignored, resulting in "geleveleg", which is a palindrome since it reads the same backward and forward.

5. Train Composition: TrainComposition.java

A TrainComposition is built by attaching and detaching wagons from the left and the right sides, efficiently with respect to time used.
For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.
Implement a TrainComposition that models this problem.

6. Circular Primes: CircularPrimes.java

Implement a function that checks a prime number is circular.
For example: 197, lets circle 971, 719 are all both primes. So 197 is a circular prime.

7. Truncatable Primes: TruncatablePrimes.java

Implement a function that checks a prime number is truncatable.
For example: 3797, left to right 797, 97, 7 and right to left 397, 37, 3 are all both primes. So 3797 is a truncatable prime

About

Solutions for common java algorithm questions

License:MIT License


Languages

Language:Java 100.0%