Code Signal
Code Signal
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing
sequence by removing no more than one element from the array.
Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence
containing only one element is also considered to be strictly increasing.
Example
For sequence = [1, 3, 2, 1] , the output should be
almostIncreasingSequence(sequence) = false .
There is no one element in this array that can be removed in order to get a strictly increasing
sequence.
Guaranteed constraints:
2 ≤ sequence.length ≤ 10 5,
-105 ≤ sequence[i] ≤ 10 5.
[output] boolean
o Return true if it is possible to remove one element from the array in order to get a
strictly increasing sequence, otherwise return false.
function almostIncreasingSequence(sequence) {
var greseli = 0;
var cur = 0;
var next = 1;
if(sequence.length == 1)
return true;
greseli++;
greseli++;
cur = next;
next++;
else
cur = next;
next++;
Rezolvare:
function almostIncreasingSequence(sequence) {
let invalidItemsCount = 0;
invalidItemsCount++;
return true;
Ex. 8
After becoming famous, the CodeBots decided to move into a new building together. Each of the
rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are
haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free r ooms, or
any of the rooms below any of the free rooms.
Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your
task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all th e values
that don't appear below a 0).
Example
For
For
A 2-dimensional array of integers representing the cost of each room in the building. A value
of 0 indicates that the room is haunted.
Guaranteed constraints:
1 ≤ matrix.length ≤ 5 ,
1 ≤ matrix[i].length ≤ 5 ,
0 ≤ matrix[i][j] ≤ 10 .
[output] integer
o The total price of all the rooms that are suitable for the CodeBots to live in.
Rezolvare:
function matrixElementsSum(matrix) {
var priceTotal = 0;
if (matrix[i][j]==0)
index.push(j);
else if (index.indexOf(j)==-1)
priceTotal=priceTotal+matrix[i][j];
return priceTotal;
}
Ex. 9:
Given an array of strings, return another array containing all of its longest strings.
Example
For inputArray = ["aba", "aa", "ad", "vcd", "aba"] , the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"] .
Input/Output
[execution time limit] 4 seconds (js)
A non-empty array.
Guaranteed constraints:
1 ≤ inputArray.length ≤ 10 ,
1 ≤ inputArray[i].length ≤ 10 .
[output] array.string
o Array of the longest strings, stored in the same order as in the inputArray.
Rezolvare:
function allLongestStrings(inputArray) {
Sau
https://www.youtube.com/watch?v=5aO-EunylgQ:
function allLongestStrings(inputArray) {
longestStrings = inputArray[i].length;
inputArray = inputArray.filter((element)=> {
});
return inputArray;
Ex. 10:
Given two strings, find the number of common characters between them.
Example
For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3 .
Strings have 3 common characters - 2 "a"s and 1 "c".
Input/Output
[execution time limit] 4 seconds (js)
[input] string s1
Guaranteed constraints:
1 ≤ s1.length < 15 .
[input] string s2
Guaranteed constraints:
1 ≤ s2.length < 15 .
[output] integer
Rezolvare:
https://www.youtube.com/watch?v=EMjv-VKAnEE
s1 = s1.split("");
s2 = s2.split("");
s1Object[s1[i]] = 1;
else {
s1Object[s1[i]]++;
s2Object[s2[i]] = 1;
else {
s2Object[s2[i]]++;
var total = 0;
else {
return total;
}
Ex. 12:
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the
sum of the first half of the digits is equal to the sum of the second half.
Input/Output
[execution time limit] 4 seconds (js)
[input] integer n
Guaranteed constraints:
10 ≤ n < 106.
[output] boolean
Rezolvare:
https://www.youtube.com/watch?v=e45wqk1zUgE
function isLucky(n) {
var firstHalfSum = 0;
var secondHalfSum = 0;
firstHalfSum += parseInt(num[i]);
}
for (var j = half; j < num.length; j++) {
secondHalfSum += parseInt(num[j]);
return true;
return false;
Ex 12
Some people are standing in a row in a park. There are trees between them which cannot be moved.
Your task is to rearrange the people by their heights in a non-descending order without moving the
trees. People can be very tall!
Example
For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be
sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].
Input/Output
[execution time limit] 4 seconds (js)
[input] array.integer a
If a[i] = -1, then the ith position is occupied by a tree. Otherwise a[i] is the height of a
person standing in the ith position.
Guaranteed constraints:
1 ≤ a.length ≤ 1000 ,
-1 ≤ a[i] ≤ 1000 .
[output] array.integer
Rezolvare:
https://www.youtube.com/watch?v=LHn8WEj3CfU
function sortByHeight(a) {
var array2 = a;
array2 = array2.filter((element)=> {
if (element != -1) {
return element;
}).sort((a,b)=> {
return a-b;
});
var index = 0;
if (a[i] != -1) {
a[i] = array2[index];
index++;
return a;
Ex. 13
Write a function that reverses characters in (possibly nested) parentheses in the input string.
Input/Output
[execution time limit] 4 seconds (js)
[output] string
o Return inputString, with all the characters that were in parentheses reversed.
Rezolvare
https://www.youtube.com/watch?v=P9osBWtIGp8:
function reverseInParentheses(inputString) {
const re = /\(([^\(\)]*)\)/g;
return inputString;
https://www.youtube.com/watch?v=IHGM97lvRGk