All Questions
Tagged with comparative-review programming-challenge
57 questions
2
votes
1
answer
154
views
LeetCode#494 target sum (dart is slower than python)
I'm trying to solve a LeetCode problem target-sum, in two languages: Python & Dart. The difference of time taken is shocking for me"
Python took ~70ms while Dart took ~900ms !! Why does Dart ...
1
vote
2
answers
121
views
Search for text in a file
I have this implementation of Grep where I use re.search:
...
5
votes
2
answers
298
views
Spliting Greeter class to multiple small classes
Write a Greeter class with greet function that receives a name as input and outputs ...
3
votes
2
answers
370
views
Debounce function that limits the rate a callback is triggered. Which implementation is better, and why?
The Debounce technique allow us to “group” multiple sequential calls in a single one - The debounce() function forces a function to wait a certain amount of time before running again.
I wrote both <...
1
vote
1
answer
404
views
Perform various actions on a list
I am solving the following HackerRank problem:
Consider a list (list = []). You can perform the following commands:
insert i e: Insert integer e at position i.
<...
3
votes
1
answer
243
views
LeetCode 1115: Print FooBar Alternately
I'm posting two solutions for LeetCode's "Print FooBar Alternately". If you'd like to review, please do. Thank you!
Problem
Suppose you are given the following code:
...
1
vote
1
answer
259
views
LeetCode 863: All Nodes Distance K in Binary Tree
I'm posting two similar solutions for LeetCode's "All Nodes Distance K in Binary Tree". If you'd like to review, please do so. Thank you!
Problem
We are given a binary tree (with root node <...
1
vote
1
answer
335
views
LeetCode 200: Number of Islands
I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!
Problem
Given a 2d grid map of '1's (land) and '0's (water), count the number
of islands. ...
1
vote
1
answer
142
views
LeetCode: House Robber I C#
https://leetcode.com/problems/house-robber/
You are a professional robber planning to rob houses along a street.
Each house has a certain amount of money stashed, the only constraint
stopping ...
4
votes
1
answer
279
views
LeetCode: Symmetric Tree C#
https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/627
Given a binary tree, check whether it is a mirror of itself (ie,
symmetric around its center).
For ...
10
votes
4
answers
5k
views
Sum Square Difference, which way is more Pythonic?
I'm using python 3 and I am creating an algorithm to find the sum square difference for the first 100 (or 1 to x) natural numbers. This happens to be project euler problem 6 if anyone is wondering.
I'...
0
votes
1
answer
270
views
Imperative and functional-programming solutions for Jumping on the Cloud challenge
Problem Statement
Emma is playing a new mobile game that starts with consecutively
numbered clouds. Some of the clouds are thunderheads and others are
cumulus. She can jump on any cumulus cloud ...
0
votes
1
answer
195
views
Adding a Node to the Linked List is taking a longer Time when using a reference to the Tail Node
I am trying to add two numbers in the form of linked List and return their result in a Linked List as given in https://leetcode.com/problems/add-two-numbers/
Question:
You are given two non-empty ...
5
votes
1
answer
8k
views
Merge Two Sorted Lists in JavaScript
The task is taken from LeetCode
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two
lists.
Example 1:
...
2
votes
1
answer
477
views
Length of the longest consecutive run of 1s in its binary representation
The task
Given an integer n, return the length of the longest consecutive run
of 1s in its binary representation.
For example, given 156, you should return 3
My function solution 1
Style 1
...
2
votes
2
answers
577
views
Python program for Jump game II
This is a Leetcode problem:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length ...
-3
votes
3
answers
1k
views
Python program to find the first missing positive integer in a list
This is a Leetcode problem:
Given an unsorted integer array, find the smallest missing positive
integer.
Note:
Your algorithm should run in O(n) time and uses constant extra space.
...
5
votes
2
answers
929
views
Create a min stack
The task
is taken from leetcode
Design a stack that supports push, pop, top, and retrieving the
minimum element in constant time.
push(x) -- Push element x onto stack.
pop() -- Removes ...
5
votes
1
answer
291
views
Container with most water
The task
is taken from leetcode
Given n non-negative integers a1, a2, ..., an , where each represents
a point at coordinate (i, ai). n vertical lines are drawn such that
the two endpoints of ...
1
vote
1
answer
366
views
Given a list and a number k return whether any two numbers from the list add up to k
The task:
Given a list of numbers and a number k, return whether any two numbers
from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 +
7 is 17.
...
4
votes
0
answers
853
views
Kadane's algorithms to leetcode "121 Best Time to Buy and Sell Stock"
I employed the conventional Kadane's algorithms to solve a maximum subarray problem in leetcode Best Time to Buy and Sell Stock - LeetCode
Description
Best Time to Buy and Sell Stock
...
4
votes
1
answer
4k
views
4 solutions to leetcode 202. Happy Number have almost same performance
I am working on an easy math question Happy number Happy Number - LeetCode
Happy Number
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by ...
4
votes
0
answers
497
views
Two solutions to leetcode 127.wordLadder
I am working on Word Ladder - LeetCode
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such ...
8
votes
3
answers
1k
views
Leetcode 38: The “count-and-say” sequence
38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:
...
3
votes
5
answers
1k
views
Finding Isogram Word
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
The ...
8
votes
4
answers
4k
views
Reversing a string - two approaches
Test Case:
Input- "I'm hungry!"
Output- "!yrgnuh m'I"
Approach 1: In this approach, I used a empty string and bind it with the input string reversely.
...
2
votes
2
answers
176
views
Project Euler #10 (sum of primes less than two million) in Clojure
I'm new to Clojure and using some Project Euler problems to familiarize myself with the language's capabilities. Problem #10 asks to find the sum of all primes less than two million.
I wanted to ...
0
votes
1
answer
1k
views
CodingBat warm up: conditionally prepending "not" to a string
I solved this CodingBat problem:
Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: ...
6
votes
2
answers
3k
views
Python Find the N adjacent digits in the 1000-digit number that have the greatest product
This question was presented to me, adapted from Project Euler #8. The goal is to find the N adjacent digits in the 1000-digit number that have the greatest product. Range of N: 10 <= N <= 50. ...
6
votes
2
answers
2k
views
Counting ambiguous Morse Code encodings, with and without Java Streams
Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ...
5
votes
4
answers
6k
views
LeetCode "Jewels and Stones": counting certain characters in a string
LeetCode "Jewels and Stones"
You're given strings J representing the types of stones that are
jewels, and S representing the stones you have. Each character in
S is a type of stone you have. ...
2
votes
2
answers
164
views
Find the middle of the string
The follow code should correctly run the test cases at the bottom.
It works correctly.
I just don't feel like this is clearest way I could write it.
A lot of the complexity comes from having to ...
3
votes
3
answers
2k
views
Project Euler Problem 5 Performance
I'm hoping for suggestions of a concise way to write my list comprehension filter in the first solution.
I would also appreciate insight on the underlying reasons for some performance differences in ...
4
votes
1
answer
3k
views
Checking if an array is unimodal
Codeforces problem 831A says:
Array of integers is unimodal, if:
it is strictly increasing in the beginning;
after that it is constant;
after that it is strictly decreasing.
The first block (...
5
votes
1
answer
181
views
Euler 1 but pointless
I've been teaching myself basic haskell this week in order to be able to better read literature about functional programming (about 80% of which in haskell by my completely off-the-cuff statistic). ...
4
votes
3
answers
2k
views
Leap year check in Haskell, using pattern matching or bind operator
One of the first Haskell puzzles on http://exercism.io/ is to implement a leap year check. I've done this twice.
Using pattern matching:
...
8
votes
4
answers
5k
views
Hackerrank : Hash Tables: Ransom Note
I just solved the Hash Tables: Ransom Note problem on Hackerrank using both Java-8 and Java-7. Given m words in a magazine and the n words in the ransom note, print ...
2
votes
0
answers
2k
views
HackerRank - Matrix Rotation
Problem statement
Given a matrix (up to 300 × 300), rotate each element R steps anti-clockwise along concentric rectangular paths (R up to 109).
The algorithm is rated as hard on HackerRank.
On ...
6
votes
2
answers
902
views
Solution to Chef and Squares challenge, timing out in Java but not in C++
I was solving a problem on codechef (online judge and contest organizer). I was writing my code in Java and was getting TLE at last testcase where as I wrote the same code in C++ and it was accepted. ...
1
vote
1
answer
120
views
Project Euler 12 (triangle numbers), solved using functools.reduce() and looping
I have solved problem 12 on Project Euler website, which reads:
The sequence of triangle numbers is generated by adding the natural
numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 +...
3
votes
1
answer
1k
views
Minimum window substring
The minimum window substring problem from leetcode.com asks:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity \$O(n)\$.
...
19
votes
2
answers
603
views
Count the occurence of nucleobases in DNA string
Inspired by this meta question I decided to take a look at Rosalind. Their first challenge seemed easy enough:
An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', ...
5
votes
1
answer
518
views
CodeEval solutions to find trailing strings
This is a problem in CodeEval, moderate level. The problem is very easy for this level. But I am not able to get more than 60 points for any solution I submit. (Moderate levels highest score is 65 and ...
5
votes
2
answers
104
views
The Geppy String: Regex vs Iteration
We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.
...
5
votes
2
answers
1k
views
HackerEarth Girlfriend's Demand challenge, solved two ways
I solved the Girlfriend's Demand challenge on HackerEarth. In summary:
Input: First line of the input contains a string S. Next line contains an integer Q, the number of queries. Each of the next ...
5
votes
1
answer
261
views
Project Euler: Problem 22 (2 versions)
I wrote 2 versions of problem 22 on Project Euler:
Using a text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value ...
3
votes
1
answer
1k
views
Project Euler #10 in Ruby. Three ways to sum primes below 2 million
Question:
Add all prime numbers smaller than 2,000,000.
The first method is done by deriving own is_prime? method:
...
3
votes
2
answers
249
views
Project Euler Retreat
Now I am doing Project Euler number 1:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the ...
7
votes
3
answers
259
views
Project Euler #5 (LCM 1 - 20) Follow up
I'm actually spending time on it, unlike the first time and going through, understanding and implementing the several suggestions I've landed on two different approaches.
Approach 1: Employing Java 8 ...
3
votes
3
answers
2k
views
General solution to sum multiples of multiple numbers below a limit
sumMultiples() is a working general solution to Project Euler's first problem. Don't read it if you want to try it yourself.
This question is about preserving ...