[Link]
com/contests/programming-interview-questions/challenges/m-th-to-last-
element
1. For this question, you will write a program that, given a positive integer M and a list of integers L, outputs the list
element M links away from the end of the list. For this program, we will use 1-indexing. That means mth_to_last(1) is the
"1st-to-last" element, or simply the last element in the list.
If you are given an invalid index, output NIL instead.
Examples
Input:
4
10 200 3 40000 5
Output:
200
Input:
2
42
Output:
NIL
General Approach
1. Construct a linked list from the inputs. While it's certainly possible to solve this using array indices, the point is to practice linked list
traversals.
2. Use that linked list to find the Mth to last element.
Things to think about
Is your list singly- or doubly-linked? How does this affect your algorithm? Does this change the complexity of your algorithm?
What if your list was circular? Would this change how you check for edge cases?
Input Format and Restrictions
Each test case will consist of two lines. The first line contains the value of M. The second line contains the values of L, each separated by
a space character.
The inputs will always satisfy the following restrictions:
0 < M < 2^32 - 1,
Each element of the list satisfies 0 <= L[i] <= 2^32 - 1,
The number of elements in the list satisfies 0 < \|L\| < 1024. The bonus test cases may be much larger!
Reading from STDIN
For help on reading from STDIN, see the HackerRank environment help page under the "Sample Problem Statement" section.
2. For this question, you will write a program that generates values from the Fibonacci sequence. The Fibonnaci sequence is recursively
defined by:
Fn = Fn - 1 + Fn - 2
Using the following seed values:
F0 = 0, F1 = 1
Given a number n, print the nth value of the Fibonacci sequence.
Examples
Input:
12
Output:
144
Input:
30
Output:
832040
General Approach
1. Find the base case(s),
2. Have your function recognize the base case(s) and provide a solution,
3. Recursively define a solution to the sub-problem for other inputs,
4. Call your function on the input and print the result to STDOUT.
Things to think about
Although we are doing this mainly to learn recursion, think about whether this is effecient in your language of choice. Does your
language support tail call elimination?
Input Format and Restrictions
Each test case will consist of a single positive integer n.
The inputs will always satisfy the following restrictions:
Fn < 2^32 - 1,
0 <= n < 50
Reading from STDIN
For help on reading from STDIN, see the HackerRank environment help page under the "Sample Problem Statement" section.
3. Find the only uncoupled integer in an array.
Problem Statement
Write a program that, given a list of integers as an argument to STDIN
n1, n2, n3, ..
Prints out the only uncoupled (unpaired) integer in the list to STDOUT.
Example 1:
Given the input
1, 2, 3, 1, 2
your program should output:
Example 2:
Given the input
1, 2, 3, 4, 5, 99, 1, 2, 3, 4, 5
your program should output:
99
4. For this question, you will parse a string to determine if it contains only "balanced delimiters."
A balanced delimiter starts with an opening character ((, [, {), ends with a matching closing character (), ], } respectively), and has only
other matching delimiters in between. A balanced delimiter may contain any number of balanced delimiters.
Examples
The following are examples of balanced delimiter strings:
()[]{}
([{}])
([]{})
The following are examples of invalid strings:
([)]
([]
[])
([})
Input is provided as a single string. Your output should be True or False according to whether the string is balanced. For example:
Input:
([{}])
Output:
True
Input Format and Restrictions
Each test case will consist of a string only containing the characters ()[]{}. The length of the string will not exceed 2KB.
Things to think about
How will you keep track of previous delimiters?
How will you determine if the next character is valid?
When you reach the end of the string, how do you know if it is balanced?
public class Solution {
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
while([Link]())
{
int a=[Link]();
[Link](a);
}
}
}