Skip to main content

All Questions

Filter by
Sorted by
Tagged with
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 ...
Abhishek Patil's user avatar
1 vote
2 answers
121 views

Search for text in a file

I have this implementation of Grep where I use re.search: ...
Eliaz Bobadilla's user avatar
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 ...
kuch's user avatar
  • 165
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 <...
Vektor's user avatar
  • 140
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. <...
Nestor's user avatar
  • 291
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: ...
Emma's user avatar
  • 3,542
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 <...
Emma's user avatar
  • 3,542
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. ...
Emma's user avatar
  • 3,542
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 ...
Gilad's user avatar
  • 5,253
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 ...
Gilad's user avatar
  • 5,253
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'...
takethelongsh0t's user avatar
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 ...
Sandio's user avatar
  • 109
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 ...
MANOJ VARMA's user avatar
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: ...
thadeuszlay's user avatar
  • 3,991
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 ...
thadeuszlay's user avatar
  • 3,991
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 ...
Justin's user avatar
  • 2,485
-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. ...
Justin's user avatar
  • 2,485
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 ...
thadeuszlay's user avatar
  • 3,991
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 ...
thadeuszlay's user avatar
  • 3,991
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. ...
thadeuszlay's user avatar
  • 3,991
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 ...
Alice's user avatar
  • 605
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 ...
Alice's user avatar
  • 605
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 ...
Alice's user avatar
  • 605
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: ...
dengApro's user avatar
  • 381
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 ...
AKdeBerg's user avatar
  • 195
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. ...
AKdeBerg's user avatar
  • 195
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 ...
brian_o's user avatar
  • 839
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: ...
aclark's user avatar
  • 13
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. ...
Durian Jaykin's user avatar
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 ...
Anirudh Thatipelli's user avatar
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. ...
Anirudh Thatipelli's user avatar
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 ...
TMB's user avatar
  • 209
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 ...
Chase G.'s user avatar
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 (...
Gaurish Gangwar's user avatar
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). ...
CAD97's user avatar
  • 1,944
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: ...
Matthias Wimmer's user avatar
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 ...
Amriteya's user avatar
  • 243
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 ...
Jianmin Chen's user avatar
  • 2,396
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. ...
lord_ozb's user avatar
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 +...
fenslett's user avatar
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)\$. ...
Lin Ma's user avatar
  • 3,453
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', ...
Mast's user avatar
  • 13.5k
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 ...
user281693's user avatar
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. ...
Caridorc's user avatar
  • 27.6k
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 ...
Ankur Anand's user avatar
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 ...
Ishaan's user avatar
  • 474
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: ...
Chris Yeung's user avatar
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 ...
user avatar
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 ...
Legato's user avatar
  • 9,839
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 ...
candied_orange's user avatar