iOS Development Made Simple!
  • Home
  • Blog
  • Contact
  • Enroll Now
  • Enroll Now

Power Of N – iOS Engineering Interview Questions And Algorithms

  • Yasser Farahi
  • April 18, 2022April 25, 2022
  • Algorithms, Interview Challenges

Today I wanted to challenge myself and try to write a function to determine if an integer is a power of N?

For example:
Input: 1
Output: true
Explanation: 2^0 = 1


Input: 16
Output: true
Explanation: 2^4 = 16

Input: 218
Output: false


Even though the Swift SDK has a built in functionality to accomplish this task, I was eager to challenge my Swift knowledge and see whether I can write this algorithm from scratch?
Could the input values be interpreted in a different way to achieve our objective?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import Foundation
 
func isPowerOf(n: Int, x: Int ) -> Bool {
    return ((_abs(x) / n) % n == .zero)
}
 
//Could also use default Swift abs() method
func _abs( _ n: Int ) -> Int {
    return n < 0 ? n * -1 : n
}
 
//Power Of Two
isPowerOf(n: 2, x: 1) //true
isPowerOf(n: 2, x: 16) //true
isPowerOf(n: 2, x: 8) //true
isPowerOf(n: 2, x: 218) //false
 
//Power Of Ten
isPowerOf(n: 10, x: 50) //false
isPowerOf(n: 10, x: 1500) // true
isPowerOf(n: 10, x: 30) //false
isPowerOf(n: 10, x: 23) //false

Tags: algoritmes Data Structure Interview Questions O(1) Runtime Power Of N Software Engineering

Post navigation

Next Post

Categories

  • Algorithms
  • Beginner
  • Courses
  • Dart
  • General
  • Interview Challenges
  • OOP
  • Programming
  • Swift

New Posts

  • iOS Development Xcode Layout And Auto Layout Tools
    August 2, 2022
  • Composition – Relations Between Objects And Classes
    June 13, 2022
  • Aggregation – Relations Between Objects And Classes
    June 8, 2022

Tags

Aggregation algoritmes Association Basics Caesar Cipher Classes Composition Data Structure Dependency Implementation Inheritance Interfaces Interview Questions O(1) Runtime O(2n) Runtime Object Relations OOP Power Of N Reverse Values Shifting Characters Shifting Values Software Engineering String Manipulation Swift Value Swapping Xcode
© Copyright RISING GALAXY. All Rights Reserved | Read Our Privacy Policy  - Terms Of Use - Cookies Policy
Cookies Policy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT