Forked from gustavohenrique/gist:40d98ad95648e7ac152c
Last active
June 27, 2018 08:44
-
-
Save SyedRaza/cf491d66cc826e2af765fcfbc1f3e479 to your computer and use it in GitHub Desktop.
HackerRank Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implement a method find that given two lists will find the starting index (indexing is zero based) where the second list occurs as a sub-list in the first list. Your implementation should return -1 if the sub-list cannot be found. Arguments are always defined, non-empty lists. | |
Sample Input 1 | |
list1 = (1, 2, 3) | |
list2 = (2, 3) | |
Sample Output 1 | |
1 | |
Explanation | |
As second list (2, 3) is sub-list in first list (1, 2, 3) at index 1 | |
Sample Input 2 | |
list1 = (1, 2, 3) | |
list2 = (3, 2) | |
Sample Output 2 | |
-1 | |
Explanation | |
As second list is not there in the first list so the output is -1 | |
function LinkedListNode(node_value) { | |
this.val = node_value; | |
this.next = null; | |
} | |
function _insert_node_into_singlylinkedlist(head, val) { | |
if(head == null) { | |
head = new LinkedListNode(val); | |
} | |
else { | |
var end = head; | |
while (end.next != null) { | |
end = end.next; | |
} | |
var node = new LinkedListNode(val); | |
end.next = node; | |
} | |
return head; | |
} | |
/* | |
* Complete the function below. | |
*/ | |
/* | |
For your reference: | |
LinkedListNode { | |
var val; | |
var next; | |
}; | |
*/ | |
function find(l, s) { | |
function toList(o) { | |
var result = []; | |
function getVal(item) { | |
result.push(item.val); | |
if (item.next) { | |
getVal(item.next); | |
} | |
} | |
getVal(o); | |
return result; | |
} | |
var list = toList(l); | |
var sublist = toList(s); | |
var indexStart = list.indexOf(sublist[0]); | |
if (indexStart < 0) { | |
return -1; | |
} | |
for (var j = 0; j < sublist.length; j++) { | |
if (list[indexStart + j] !== sublist[j]) { | |
return -1; | |
} | |
} | |
return indexStart; | |
} | |
------- | |
Let's define a function "LookAndSay" as follow: read off the digits of the input, counting the number of digits in groups of same digit. Here are some examples of this function | |
LookAndSay(1) = 11 because 1 is read off as "one 1" or 11. | |
LookAndSay(11) = 21 because 11 is read off as "two 1s" or 21. | |
LookAndSay(21) = 1211 because 21 is read off as "one 2, then one 1" or 1211. | |
LookAndSay(1211) = 111221 because 1211 is read off as "one 1, then one 2, then two 1s" or 111221. | |
LookAndSay(111221) = 312211 because 111221 is read off as "three 1s, then two 2s, then one 1" or 312211. | |
We then define a "Look and Say" sequence as repeatedly called the "Look and Say" function on its output. | |
Given a number start and a number of iteration n, calculate the nth number in a "Look and Say" sequence starting with start. | |
Reusing the previous example with start = 11 and n = 2, LookAndSay(11, 2) = 1211 because LookAndSay(LookAndSay(11)) = 1211 | |
function LookAndSay(start, n) { | |
} | |
---- | |
"You have to complete the function convert that takes an integer as its argument and returns the encoded string in base 7 using the following encoding rule: | |
base 10 0 1 2 3 4 5 6 | |
base 7 0 a t l s i n | |
Constraints | |
0 <= input <= 1012 | |
Sample Input 1 | |
7 | |
Sample Output 1 | |
a0 | |
Sample Input 2 | |
7792875 | |
Sample Output 2 | |
atlassian | |
function convert(in) { | |
} | |
--------- | |
We have a robot that can pick up blocks from a stash, move them horizontally, and lower them in place. There are 10 positions available to lower blocks, numbered from 0 to 9. Each position can hold up to 15 blocks. | |
The robot understands the commands 'P', 'M' and 'L': | |
P: Pickup from the stash and move to position 0 | |
M: Move to the next position | |
L: Lower the block | |
The robot is safe to operate and very forgiving: | |
- There are always blocks in the stash (Pickup will always get a block). | |
- If the robot already holds a block, Pickup will reset the robot to position 0. | |
- The robot will not go beyond position 9. Trying to Move it further does nothing. | |
- Lowering a block on a pile of 15 blocks does nothing (and the robot will keep any block it holds). | |
- Lowering without a block does nothing. | |
- The robot ignores any command that is not 'P', 'M' or 'L'. | |
Implement a function that takes a String of commands for the robot. The function should output a String representing the number of blocks (in hexadecimal) at each position after running all the commands. | |
Sample Input 1 | |
String = "PMLPMMMLPMLPMML" | |
Sample Output 1 | |
"0211000000" | |
Sample Input 2 | |
"PLPLPLPLPLPLPLPLPLPL" | |
Sample Output 2 | |
"A000000000" | |
function compute(instructions) { | |
} | |
------- | |
A third-party provider uses a custom data format to exchange information with us. We need to validate the input before we process it further. | |
The character encoding is US-ASCII. Valid characters are all the characters between 0x20 (space) and 0x7E (~). | |
All fields are delimited by '|', and '~' is the escape character. There are only three valid escape sequences: | |
'~|' stands for '|' | |
'~~' stands for '~' | |
'~n' stands for new line | |
A line represents one record, it must start and end with '|'. | |
The first line contains the field names. The remaining lines contain the records. Names can't be empty and must be unique, there is no restriction on values. | |
If a record has more fields than there are names defined, the last name defined will be used and '_#' will be appended to the field name where # is the number of extra record starting at 1. | |
Here is a valid example: | |
|name|address|~n|Patrick|[email protected]|[email protected]|~n|Annie||[email protected]|~n | |
This represents the following data: | |
name address address_1 | |
Patrick [email protected] [email protected] | |
Annie [email protected] | |
Valid input should output statistics about the data: number of records, number of fields, number of empty values and the name of the last field. For the previous example, the following output is expected: "2:3:1:address_1" | |
Invalid input (such as '~~~') should output the following message "0:0:0:format_error" | |
You have to write a validate function which verifies the input string conforms to the requirements and generates either the expected output or the error message. | |
function validate(input) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment